0

I present a UIViewController out, which uses autolayout to set the constraints for the sub views inside the view of the view controller. all the constraints are kept in the code, without storyboard or xib.

I add the safeAreaLayoutGuide inside the code, such as below:

NSLayoutConstraint *const containerViewTopConstraint = [self.containerView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor];

the problem is after I rotating from portrait to landscape, the view.safeAreaLayoutGuide didn't get updated. which is still the old safeAreaLayoutGuide in the portrait. wonder if I miss something? Thanks!

whitekevin
  • 311
  • 3
  • 15
  • You need to provide a bit more information. Can you show this with just a single subview? Can you show screen-caps of what's happening that you're not expecting? – DonMag Jun 05 '20 at 19:03
  • @DonMag, Thanks for reply, the un expecting thing is when I rotate to landscape, the it will add extra margin in the top, but with no margin in the left and right side, then my view will overlay with the notch of iphone X. – whitekevin Jun 05 '20 at 20:45

2 Answers2

0

It sounds like you're misinterpreting the concept of "top"?

The layout guide does not "rotate with the device." When you rotate your phone, the self.view.safeAreaLayoutGuide.topAnchor is no longer the edge with the notch.

In these image, all 4 edges of the cyan label (top / bottom / leading / trailing) are constrained to the view's safeAreaLayoutGuide:

enter image description here

enter image description here

In these images, only the Top edge is constraint to the view's safeAreaLayoutGuide - leading / trailing / bottom are constrained to the view's respective anchors:

enter image description here

enter image description here

DonMag
  • 69,424
  • 5
  • 50
  • 86
0

Turns out I found the reason, which is very silly, my teammate wrote the wrong code...

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidDisappear:animated];
    ...
}

which cause the safeAreaLayoutGuide not be updated after rotating.

Thanks all your guys answer.

whitekevin
  • 311
  • 3
  • 15