fittingSize()

This commit is contained in:
relikd
2020-07-02 12:26:34 +02:00
parent 4f92d3d58d
commit 723f1665a7
2 changed files with 14 additions and 10 deletions

View File

@@ -172,18 +172,10 @@ private class StickyPresentationController: UIPresentationController {
let preferred = presentedViewController.preferredContentSize
switch stickTo {
case .left, .right:
let fitted = target.systemLayoutSizeFitting(
CGSize(width: preferred.width, height: full.height),
withHorizontalFittingPriority: .fittingSizeLevel,
verticalFittingPriority: .required
)
let fitted = target.fittingSize(fixedHeight: full.height, preferredWidth: preferred.width)
return CGSize(width: min(fitted.width, full.width), height: full.height)
case .top, .bottom:
let fitted = target.systemLayoutSizeFitting(
CGSize(width: full.width, height: preferred.height),
withHorizontalFittingPriority: .required,
verticalFittingPriority: .fittingSizeLevel
)
let fitted = target.fittingSize(fixedWidth: full.width, preferredHeight: preferred.height)
return CGSize(width: full.width, height: min(fitted.height, full.height))
}
}

View File

@@ -17,6 +17,18 @@ extension UIView {
return UIImage(cgImage: image!.cgImage!)
}
}
/// Find size that fits into frame with given `width` as precondition.
/// - Parameter preferredHeight:If unset, find smallest possible size.
func fittingSize(fixedWidth: CGFloat, preferredHeight: CGFloat = 0) -> CGSize {
systemLayoutSizeFitting(CGSize(width: fixedWidth, height: preferredHeight), withHorizontalFittingPriority: .required, verticalFittingPriority: .fittingSizeLevel)
}
/// Find size that fits into frame with given `height` as precondition.
/// - Parameter preferredWidth:If unset, find smallest possible size.
func fittingSize(fixedHeight: CGFloat, preferredWidth: CGFloat = 0) -> CGSize {
systemLayoutSizeFitting(CGSize(width: preferredWidth, height: fixedHeight), withHorizontalFittingPriority: .fittingSizeLevel, verticalFittingPriority: .required)
}
}
extension UIEdgeInsets {