Migrating a project using autoresizing masks for iPhone X

女生的网名这么多〃 提交于 2019-12-12 08:59:09

问题


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.


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/46319388/migrating-a-project-using-autoresizing-masks-for-iphone-x

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