简体   繁体   中英

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.

Here's how I solved this with my legacy project that used XIB files and autoresizing layout:

  1. In Interface Builder, enable auto-layout for the XIB and turned on Safe Areas.
  2. Select all UI elements in the view and then Editor->Embed In->View. This trick preserves the autoresizing settings for the selected elements.
  3. In the new UIView created, using Auto-layout set the top, leading, bottom, and trailing edges to the superview Safe Area.

This worked great for my project to quickly support Safe Areas in my many XIB files without having to change from autoresize to auto layout.

Wes

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;
    }
}

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