3 Replies
      Latest reply on May 14, 2019 10:08 AM by willy.moral
      DrMiller Level 1 Level 1 (0 points)

        Updating an old app and for all kinds of reasons NOT using AutoLayout. My code works fine without any "magic" numbers about screen size, checking for individual sizes or devices, etc. and it works fine for everything EXCEPT iPhone X. So, I need to confine two sides of my main view to a safe areas (landscape - notch side and bottom) and the problem is that one of my custom views is created during the app's view life cycle, and the safeAreaInsets that I need to use are not around when I need them to create the view.

         

        I've seen code around where depending on whether iOS 11 is available, constraints can be added programmatically to limit the view to the safe area and if not iOS 11 just stick to the sides (or do nothing).

         

        Two questions. 1) can working constraints be added programmatically to a NON-AutoLayout environment, in other words, are the safe area guides even there and will constraints work when not using AutoLayout, and 2) is there any other way to *simply* deal with iPhone X (without AutoLayout in IB and the safe area guides)?

        • Re: Safe Area Constraints without AutoLayout?
          hotwing Level 1 Level 1 (0 points)

          I also do not use autolayout or storyboards and never plan to unless absolutely forced to. I grab the safeAreaInsets from the application window and simply adjust the position of my views in viewWillLayoutSubviews. What I will probably do in future is just make subclass of UIViewController which adds a container UIView that adheres to safe area and just align my controls with respect to the container view.

           

          -(void)viewWillLayoutSubviews{
              float bottomSafeAreaInset = 0;
              if (@available(iOS 11, *)) {
                  UIEdgeInsets inset = [[UIApplication sharedApplication] delegate].window.safeAreaInsets;
                  bottomSafeAreaInset = inset.bottom;
              }
            
              [palette setFrame:CGRectMake(0, self.view.frame.size.height-(palette.frame.size.height+bottomSafeAreaInset), self.view.frame.size.width, palette.frame.size.height)];
          }