iOS 10,Swift 4.1,将视图的顶部锚设置为topLayoutGuide底部锚导致冲突

时间:2018-09-19 20:37:52

标签: ios swift ios10 uilayoutguide

我试图将视图的位置设置为状态栏下方。它可以与iOS 11+中的safeAreaLayoutGuide配合使用,但我尝试使用在网上找到的建议<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)(正在运行此代码,并带有对父控制器的引用)

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

1 个答案:

答案 0 :(得分:1)

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