问题
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?
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.
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:
- In Interface Builder, enable auto-layout for the XIB and turned on Safe Areas.
- Select all UI elements in the view and then Editor->Embed In->View. This trick preserves the autoresizing settings for the selected elements.
- 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