Search code examples
iosobjective-ciphoneiphone-x

Old app to support for iPhone X?


My old iOS app works well in all iPhone versions(Except iPhone X model). I run into iPhone X simulator and seeing some of the screens gets overlapped at bottom of iPhone X screen. My apps all uiviewcontrollers created programmatically, not designed by story board. So in all view controllers I used (self.view.frame) to find its frame size. In iPhoneX there is safe guard area to design in story board. But how do I calculate safe guard area in runtime. While searching google it gives some sample to find safe guard area which is support from iOS11.. But I want to run my app from ios9? I have lot of uiviewcontrollers in my project .. So can anyone tell me an easiest way to do this task?


Solution

  • One possible way to add a run-time fix for iPhone X layout issues to old (pre-storyboard manual layout) apps (with old pre-11 Deployment targets), is to add Objective C code similar to:

    CGRect r0 = self.view.frame;
    CGRect r1 = r0;
    if (@available(iOS 11.0, *)) {
        r1 = self.view.safeAreaLayoutGuide.layoutFrame;     
    }
    if ( r1.size.height < (r0.size.height - 20.0) ) {
        // fix top and bottom view overlap outside safe area here
    }
    

    inside each UIViewController's viewWillLayoutSubviews method.

    The @available check will make this code safe to use under iOS versions prior to iOS 11.