1

我正在尝试将视图的位置设置在状态栏下方。它适用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) (运行此代码的位置,并引用了父控制器)

子视图是在代码中添加的,并且可以放置在屏幕上的不同位置,在这种情况下它位于顶部中心。

4

1 回答 1

1

您的子视图控制器视图的顶部布局指南不会引用您的父视图控制器的顶部布局指南。就像您使用安全区域插图的布局指南一样,如果您参考父视图控制器的视图布局指南,您应该没问题。我猜想冲突是因为你添加了第二个约束,说“我的视图低于我的顶部布局指南”,默认情况下已经是这种情况

于 2018-09-20T08:09:08.750 回答