我正在尝试将视图的位置设置在状态栏下方。它适用safeAreaLayoutGuide
于 iOS 11+,但我正在尝试使用我在网上找到的 < 11 建议。
这是我正在尝试的。
if #available(iOS 11.0, *) {
let guide = parentController.view.safeAreaLayoutGuide
NSLayoutConstraint.activate([view.topAnchor.constraintEqualToSystemSpacingBelow(guide.topAnchor, multiplier: 1.0),
view.centerXAnchor.constraint(equalTo: parentController.view.centerXAnchor)])
} else {
NSLayoutConstraint.activate([view.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor),
view.centerXAnchor.constraint(equalTo: parentController.view.centerXAnchor)])
}
在 iOS 10 的情况下,我遇到了这个问题(如下),不确定这个冲突来自哪里。
2018-09-19 13:31:20.097129 iOSTestAppSingleWorkspace[70650:12307124] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<_UILayoutSupportConstraint:0x60000028d020 _UILayoutSpacer:0x6000001ba4e0'UIVC-topLayoutGuide'.height == 20 (active)>",
"<_UILayoutSupportConstraint:0x60000028ce40 V:|-(0)-[_UILayoutSpacer:0x6000001ba4e0'UIVC-topLayoutGuide'] (active, names: '|':UIView:0x7f92864249c0 )>",
"<NSLayoutConstraint:0x60000028d0c0 V:[_UILayoutSpacer:0x6000001ba4e0'UIVC-topLayoutGuide']-(0)-[UIView:0x7f92864249c0] (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60000028d0c0 V:[_UILayoutSpacer:0x6000001ba4e0'UIVC-topLayoutGuide']-(0)-[UIView:0x7f92864249c0] (active)>
层次结构是带有子视图控制器的 UIViewController(parentController) (运行此代码的位置,并引用了父控制器)
子视图是在代码中添加的,并且可以放置在屏幕上的不同位置,在这种情况下它位于顶部中心。