Autolayout, best place to activate landscape/portrait orientation constraints when app is launched in landscape...

updateViewConstraints seems to be called too early. If I launch the app in landscape on iPad, I initially get portrait constraints until I rotate.


viewWillAppear: ...if I do it, no dice. Too early?


viewDidAppear: if I do it..... the constraints update...but it's nasty, because the view already appeared. also stuck it in viewWillLayoutSubviews...before the call to super. If I launch the app in landscape I still get portrait only constraints until viewDidAppear.


-(void)doUpdateConstraints
{
    if (self.view.frame.size.width < self.view.frame.size.height)
    {
       //in portrait...disable landscape traits first...otherwise autolayout may throw
        for (NSLayoutConstraint *constraint in self.landscapeConstraints) {constraint.active = NO; }
        for (NSLayoutConstraint *constraint in self.portraitConstraints) { constraint.active = YES; }
    }
    else
    {
        //in landscape...disable portrait first...otherwise autolayout may throw.
        for (NSLayoutConstraint *constraint in self.portraitConstraints) { constraint.active = NO; }
        for (NSLayoutConstraint *constraint in self.landscapeConstraints) { constraint.active = YES; }
    }
}

-(void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
  [self doUpdateConstraints];
}

-(void)updateViewConstraints
{
  [super  updateViewConstraints];
   [self doUpdateConstraints];
}

When running on an iPhone 6 plus and I launch in landscape, the call in viewDidAppear: seems to work (that is, the constraints are changed before the app is visible).

I shouldn't have to stuff these portrait/landscape specific constraints in array...I should be able to do this in IB...but there is no way to differentiate iPad portrait and iPad landscape from size classes...too bad we can't customize size classes and set them up the way we want for our designs.


Anyway, if I call the method in viewDidAppear: I don't see a nasty snap on the 6 plus like I do in the simulator. I'll have to try on my iPad 2 later. viewWillAppear: seems like it should be the place to make the call, but for whatever reason it's not.

hmmm...when a view presents modallly...like Intersitial iad....if you rotate the device then dismiss the modal....the constraints are broke until you rotate the device.


How do you handle that?


Also notice during the slide up animation...my custom view I use as a navbar squeezes down about 20 points or so, like the navigation controller is shrinking my view controller's view in the container or something. yikes. (Note this only happens when it prsents from landscape). It looks like the autolayout engine is telling my view controller it's portrait because the view controller it's presenting is presenting in portrait.


Edit: Actually before the 'slide up' modal animation, my view controller switches from portrait to landscape just before the presentation. The 'gaps' I'm seeing is because some subviews are hidden in landscape, but not in portrait. The constraints are updating to the 'portrait' constraints but the problem is that the subviews aren't being shown. I set the hidden property to NO on all those subviews prior to the presentation...but they still aren't drawn on screen. It's like the presentation is taking a snapshot too early. This seems to only occur when embedded inside a navigation controller. Filed a bug:

21486584

Size Classes account for orientation. When you change to a different size class there is an option to select iPhones/iPad in portrait/landscape. At that time (once you've picked a new size class), you can remove all existing constraints and add new ones that are appropriate for the size class in question. But before doing this make sure you've completed the any/any autolayout first.

I know size classes account for orientation, but that doesn't seem to be the case for the iPad. Unless I'm mistaking, isn't iPad always regular x regular?

Autolayout, best place to activate landscape/portrait orientation constraints when app is launched in landscape...
 
 
Q