I have noticed an issue where I resize a View, but the bounds do not appear to change (so after pinching a view you have to use the edge of a UIView to pinch further)
@IBAction private func handlePinch(_ sender: UIPinchGestureRecognizer) {
if let view = sender.view {
view.transform = view.transform.scaledBy(x: sender.scale, y: sender.scale)
sender.scale = 1
}
}
I found a likely stackoverflow answer iphone uiview - resize frame to fit subviews that gave an extension to resize to fit subviews, but there is no observable difference in the behavior of the objects.
@IBAction private func handlePinch(_ sender: UIPinchGestureRecognizer) {
if let view = sender.view {
view.transform = view.transform.scaledBy(x: sender.scale, y: sender.scale)
sender.scale = 1
}
self.resizeToFitSubviews()
}
extension UIView {
func resizeToFitSubviews() {
let subviewsRect = subviews.reduce(CGRect.zero) {
$0.union($1.frame)
}
let fix = subviewsRect.origin
subviews.forEach {
$0.frame.offsetBy(dx: -fix.x, dy: -fix.y)
}
frame.offsetBy(dx: fix.x, dy: fix.y)
frame.size = subviewsRect.size
}
How can I make the pinch (and ultimately the handleRotate function with the same issue) work to adjust the bounds correctly?