Auto layout invalid after presenting view controller (and many situation)

6 Solutions Collect From Internet About “Auto layout invalid after presenting view controller (and many situation)”

I had a similar problem. I was setting translatesAutoresizingMaskIntoConstraints = NO; on my root UIView.
It appears the “outermost” UIView – the superview at the root of your hierarchy must use the default translatesAutoresizingMaskIntoConstraints = YES.
Once I’ve removed this, everything worked as expected.

It is probably caused by ambiguous constraints. I recommend pause a running application and type a following command to console:

po [[UIWindow keyWindow] _autolayoutTrace] 

I have also the same problem and when i tried this navigation controller it was working fine but not with present viewcontroller. Use this below view controller method along with translatesAutoresizingMaskIntoConstraints to solve this problem.

-(void) viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    self.view.translatesAutoresizingMaskIntoConstraints = NO;
}

Please let me now it you have any concern about it. Thanks

If you use auto layout between the UIWindow and your root view (to set the root view to fill the entire UIWindow) these constraints will get blown away by a full screen presentation of another view controller.

What happens, is the entire hierarchy of the full-screen presentation replaces everything below UIWindow – your original view is removed (blowing away the constraints) and the new view hierarchy substituted. Then when your review is replaced, these constraints are lost. You would need to recreate them somewhere like viewWillAppear: or just ensure your root view has self.view.translatesAutoresizingMaskIntoConstraints = NO;

I had a similar problem with table view cells that I was laying out with autolayout. Upon returning from a modal view, the cells had an invalid layout. I could re-layout each cell after viewDidAppear, but it looked terrible. Thanks to @palimondo’s suggestion I started sniffing around translatesAutoresizingMaskIntoConstraints. It turns out that I was setting the content view of my table view cell to translatesAutoresizingMaskIntoConstraints = NO when I shouldn’t have been.

I had the same problem, with a search bar container as the misplaced element (inside a UIViewController, inside a UITabBarController). None of the other solutions I’ve seen worked, but I eventually managed to work around it by re-adding the layout constraint in the viewWillAppear:

[self.view addConstraint:[NSLayoutConstraint constraintWithItem: self.searchBar.superview
                                                      attribute: NSLayoutAttributeTop
                                                      relatedBy: NSLayoutRelationEqual
                                                         toItem: self.view
                                                      attribute: NSLayoutAttributeTop
                                                     multiplier: 1
                                                       constant: 0]];