Migrating a project using autoresizing masks for iPhone X

别来无恙 提交于 2019-12-04 12:21:40

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!