iOS 9 xib和storyboard的Safe Area Top约束不同

iOS 9 Safe Area Top constraint of xib and storyboard is different

在 iOS 9,iPhone 5,没有发生在 iOS 10 和以后

UIViewController 从 storyboard 创建,我设置 viewController 的红色子视图 view.top 等于安全区域顶部,红色视图就在 statusBar 下方。

如果从 xib 文件创建 viewController,使用 init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) 并设置红色子视图的 view.top 等于安全区域顶部,红色视图与 origin.y 相同状态栏。如下:

从storyboard和xib创建的viewController是相同的class,可能从storyboard创建和xib有一些不同的设置,我尝试取消select所有storyboard select 选项,但还没有找到有区别的选项。

谁能解释一下?谢谢!

这个问题的回购:https://github.com/codingingBoy/SafeAreaDemo

旧 iOS 版本的 XIB 和安全区指南存在向后兼容性问题,因为 XIB 文件没有它们。

In the current Xib backwards deployment behavior (iOS 10 and prior), a constraint to the safe area will be converted to the superview, because a XIB does not have top and bottom layout guides.

This may not achieve the layout behavior that you want, and we recommend you working around this case with one of the following options:

1) move the the view and layout to a storyboard view controller

2) use IB Outlets to the relevant (top and bottom) constraints, and programatically replace them with constraints to top/bottom layout guide once the view gets installed

3) install the view as a subview, and programmatically add constraints to top/bottom layout guides and sides.

参考:https://forums.developer.apple.com/thread/87329