iOS10、swift4.1、设置view的top anchor为topLayoutGuide bottom anchor导致冲突

iOS 10, swift 4.1, setting view's top anchor to topLayoutGuide bottom anchor causing conflicts

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

子视图已添加到代码中,可以放置在屏幕上的不同位置,在本例中它位于顶部中央。

您的子视图控制器视图的顶部布局指南将不会引用您的父视图控制器的顶部布局指南。就像您使用安全区域插图的布局指南所做的那样,如果您引用父视图控制器的视图布局指南,您应该没问题。我想冲突的产生是因为你添加了第二个约束 "my view is below my top layout guide" 默认情况下已经是这种情况