简体   繁体   English

使用iPhone X的自动调整掩码迁移项目

[英]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? 我们有一些仍在使用Autoresizing mask的遗留项目,在iOS 11和iPhone X之前一切正常。随着安全区域布局指南的引入,支持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. 我们继续使用自动调整遮罩,但调整界面以添加iPhone X和iOS 11的嵌入边距。

Here's how I solved this with my legacy project that used XIB files and autoresizing layout: 以下是我使用XIB文件和自动调整布局的遗留项目解决这个问题的方法:

  1. In Interface Builder, enable auto-layout for the XIB and turned on Safe Areas. 在Interface Builder中,为XIB启用自动布局并启用“安全区域”。
  2. Select all UI elements in the view and then Editor->Embed In->View. 选择视图中的所有UI元素,然后选择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. 在创建的新UIView中,使用自动布局将顶部,前导,底部和尾部边缘设置为超视图安全区域。

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. 这对我的项目非常有用,可以快速支持我的许多XIB文件中的安全区域,而无需从自动调整大小更改为自动布局。

Wes 韦斯

A third option would be to use autolayout where you need to and leave the rest of the app with autoresizing. 第三种选择是在需要的地方使用autolayout,并使用autoresizing离开应用程序的其余部分。 Since XCode 8, you may mix autoresizing and autolayout. 从XCode 8开始,您可以混合自动调整和自动布局。 For each view in the xib or storyboard, you may choose to set an autoresizing mask or autolayout constraints. 对于xib或storyboard中的每个视图,您可以选择设置自动调整遮罩或自动布局约束。 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/ 此链接提供了更多信息: 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. statusBarHeight为您提供设备状态栏的高度。 safeAreaBottomMargin gives you the bottom margin left for iPhoneX's home button indicator. safeAreaBottomMargin为您提供iPhoneX主页按钮指示器的左下边距。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM