0

I've a series of cells that resize using a UIPinchGestureRecognizer with something like

pinchGesture.view?.transform = (pinchGesture.view?.transform)!.scaledBy(x: deltaScale, y: deltaScale)

and it works fine. However, I had some buttons and a label on the cell that appear when the cell is tapped, and these would resize with the superview when pinched to zoom (so the buttons would be too small to press / large not conforming to the design).

So I programmatically added a view to my cells when tapped to solve this:

let myView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
myView.backgroundColor = UIColor.red
var label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
label.text = "I'm a test label"
myView.addSubview(label)

I added

myView.translatesAutoresizingMaskIntoConstraints = false

But the subview myView still resizes!

How can I stop the subview "myView" from resizing?

stevenpcurtis
  • 1,907
  • 3
  • 21
  • 47
  • It looks to me like you are confusing a few things. (1) Why are you combining auto layout constraints with frames? That's a major no-no. (2) Why are you thinking that setting the autoresizing mask does anything besides allow for constraints to work? (3) Now, assuming you have things consistent - preferably using auto layout throughout - set additional constraints with priorities and/or "greater/less than or equal to" to let the layout engine know what you need. –  Aug 19 '17 at 09:38
  • You can say that, but with my original (non programmatic) solution I've added a stackview in a storyboard with buttons - constraints are centerX = superview.centerX and height = 168; yet still the stackview is stretched with the superview is. I understand that i this case I'm using autolayout throughout. – stevenpcurtis Aug 19 '17 at 09:47
  • My bad. You see, you *didn't* mention it was a `UIStackView`. I thought it was a `UITableView`. Stack views absolutely tromp (at least sometimes) on code because they try to add auto layout constraints where one doesn't expect! (At least in my experience. I tend to skip by stack/collection views and IB and set all my constraints in code.) –  Aug 19 '17 at 09:50
  • What I've done is tried to simplify the problem programmatically above. By adding a simple UIView (with UILabel subview) everyone can see the issue - The text changes size. If I can do such in code, I can solve this on the storyboard (or vice versa). The target result is with a UIStackView, but ultimately the question is how to prevent ANY subview in a UIVIew from resizing when the superview does, via a punchgesture. – stevenpcurtis Aug 19 '17 at 09:53
  • Did you find a solution for this? – FredFlinstone Jul 26 '18 at 11:16
  • Not directly. When zooming / pinching I remove the elements I do not want resized, then add them back when the resizing is complete. Not great, but a solution. – stevenpcurtis Jul 26 '18 at 11:27

0 Answers0