iosios11autoresizingmasksafearealayoutguide

Migrating a project using autoresizing masks for iPhone X


We have some legacy projects that are still using Autoresizing masks, and everything was working fine until iOS 11 and iPhone X. With the introduction of safe area layout guides, what's the best solution to support iPhone X?

  1. We can convert all interfaces with autoresizing masks to use auto-layouts. This appears to be a significant effort, considering views are being dynamically added and adjusted.

  2. We keep using autoresizing masks but adjust the interfaces to add inset margins for iPhone X and iOS 11.


Solution

  • A third option would be to use autolayout where you need to and leave the rest of the app with autoresizing. Since XCode 8, you may mix autoresizing and autolayout. For each view in the xib or storyboard, you may choose to set an autoresizing mask or autolayout constraints. Using one kind of rules on a view disables the other kind for that view. But you may use the other kind on another view. This link has some more information : http://www.thomashanning.com/xcode-8-mixing-auto-autoresizing-masks/

    If you choose to keep using only autoresizing masks, the helper methods below may help you to layout your views correctly.

    statusBarHeight gives you the height of the status bar for the device. safeAreaBottomMargin gives you the bottom margin left for iPhoneX's home button indicator.

    - (CGFloat) statusBarHeight {
        return UIApplication.sharedApplication.statusBarFrame.size.height;
    }
    
    - (CGFloat) safeAreaBottomMargin {
        if (@available(iOS 11.0, *)) {
            UIWindow *window = UIApplication.sharedApplication.windows.firstObject;
            return window.safeAreaLayoutGuide.owningView.frame.size.height - window.safeAreaLayoutGuide.layoutFrame.size.height - window.safeAreaLayoutGuide.layoutFrame.origin.y;
        } else {
            return 0;
        }
    }