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

1k Views Asked by At

I am trying to set my view's position to be under the status bar. It works with the safeAreaLayoutGuide in iOS 11+ but I'm trying to use a suggestion I found online for < 11.

Here is what I'm trying.

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)])
}

In the case of iOS 10 I am getting this issue (below) not sure where this conflict is coming from.

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)>

The hierarchy is a UIViewController(parentController) with a child view controller (where this code is running, with a reference to the parent controller)

The child view is added in code, and could be placed in different positions on screen, in this case it's in the top center.

1

There are 1 best solutions below

0
On BEST ANSWER

The top layout guide of your child view controllers view will not reference the top layout out guide of your parent view controller. Just like you did it with the layout guide for the safe area insets, you should be fine if you reference your parent view controller's view's layout guide. I guess the conflict is arising because your adding a second constraint that says "my view is below my top layout guide" which is already the case by default