简体   繁体   中英

iOS Autolayout rotation App crash

  1. When I launch the app in portrait mode and I rotate the screen to landscape, then the constraints are working fine.

  2. When I launch the app in portrait mode and presented an model view controller and i dismissed the model view controller. then I rotate the screen to landscape the constraints are not working.

  3. When I launch the app and rotate to landscape mode and when I present the model view controller, App gets crashed, crash report is followed,

The Model view controller contains navigation bar with constraints (superview.trailing with navigationbar.trailing, superview.leading with navigationbar.leading and navigationbar.top with TopLayoutGuide.Bottom ). all are 0 constants.

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Autolayout doesn't support crossing rotational bounds transforms with edge layout constraints, such as right, left, top, bottom. The offending view is: <UITransitionView: 0x8dd5300; frame = (0 0; 320 568); transform = [0, -1, 1, 0, 0, 0]; clipsToBounds = YES; autoresize = H; layer = <CALayer: 0x8dd3980>>

Step. Create a new Single view based app and add a imageView Called _backgroundImageView and and the below constraints.

NSLayoutConstraint *backgroundImageConstraints1 = [NSLayoutConstraint constraintWithItem:_backgroundImageView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];

NSLayoutConstraint *backgroundImageConstraints2 = [NSLayoutConstraint constraintWithItem:_backgroundImageView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];

NSLayoutConstraint *backgroundImageConstraints3 = [NSLayoutConstraint constraintWithItem:_backgroundImageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1 constant:0];

NSLayoutConstraint *backgroundImageConstraints4 = [NSLayoutConstraint constraintWithItem:_backgroundImageView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1 constant:0];

Then add a button which takes to second view controller as PresentModelView. the second view Contains only NavigationBar which shown the image Below.

SecondViewController屏幕

I usually don't do auto layout programmatically, but heres what i would do in your case.

  1. create a snapshot of your project.
  2. delete any and all code pertaining to auto layout.
  3. Go into your storyboard file and, after selecting the UI element that you want to add constraints to, click on the 'add constraints' button.

在此输入图像描述

  1. Edit the information in the popup box and click 'add constraints' when done.

repeat for each UI element you want to use constraints on


By re-doing auto layout this way, you will dramatically lessen the chance of introducing errors into your code as described by your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM