2

I have a UIViewController which I add to another UIViewController as a child. When the child gets added, the parent set frame on my child using childVC.frame = ...., but this fubars my constraints.

I can duplicate this issue in IB easily. Create a UIViewController, change the size to be "Freeform" and ditch the status bar (just so it looks right visually). Add in a UIButton (or anything) and constraints so so they are both the same size (see image below) enter image description here

Now change the size of the top level view in the view controller and watch what happens. enter image description here

It changed my constraints on me. I wanted my button to just get bigger and still hold the original constraints. Something which happens automatically if you toggle between iPhone retina 3.5/4.0 in screen or change from landscape to portrait. But why not when I resize the view? This is making a royal pain to lay things out in freeform and test different sizes.

More importantly... why does the exact same thing happen when I set the frame in code when I add the child for my VC and how do I prevent it?

4
  • My only comment on IB is that IB's constraints editing is crap - bit you're not "reproducing" the issue by doing that. In an Autolayout world, you'd never setFrame: - everything is constraints based. So you should be editing the constraints that apply to your child view control let's view, not setting its frame. Commented Nov 24, 2012 at 8:49
  • I just want some way in IB to say "my superview will be width/height" and have it re-display with the existing constraints. Next I'm going to try mucking in the XML file. Maybe if I modify my view controller width/height values there it will leave my constraints untouched. Probably not, but worth a test. Commented Nov 27, 2012 at 14:27
  • Have you tried just dragging the superview to the size you want? Don't set the frame. Commented Dec 17, 2012 at 23:48
  • I have, it changes the constraints rather than re-applying the existing constraints to the new new size. Commented Dec 19, 2012 at 13:58

1 Answer 1

1

Frame based calculations and Auto Layout are not compatible. Or at least, they mean stormy waters. That's why you are having issues. Having switched to Auto Layout you have to accept that you no longer manipulate frame directly.

So, you cannot do childVC.frame = whatever.

Instead, you must now use Auto Layout to update the size of childVC.frame. Do that by creating - in IB - an IBOutlet that is the width constraint of childVC. Similarly, create another IBOutlet that is the childVC height constraint.

In your view controller, update the constant value to change the size.

myWidthConstraint.constant = 123.f;

The place to do this is in the controller's updateViewConstraints method, or in the case of a view, updateConstraints.

That said, it's better to set up Auto Layout so that you don't need any programmatic updates. When properly defined, it will know how to handle things with the view's bounds change. To be honest, it should only be necessary to update sizes programmatically if you want to do something more unusual.

Can I just say however that you are better

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.