1

I have this TabBarController in my app to which I've added an UIView on the very top of the TabBar. I did this because this view shows persistent data that I wanted to display in every view. I initialized this using initWithFrame: and it is just where I wanted it to be.

Today I noticed that when the status bar increases in height (during a call or personal hotspot), that view moves towards the bottom. I tried to fix that by adding a constraint:

[[self view] addConstraints:[NSLayoutConstraint 
                     constraintsWithVisualFormat:@"V:[_myView]-(44)-|"
                                         options:NSLayoutFormatDirectionLeadingToTrailing
                                         metrics:nil
                                           views:NSDictionaryOfVariableBindings(_myView)]];

This way, I'd expect that myView would always be spaced by 44 pts from the bottom of the screen, however it does not happen.

The debugger shows:

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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)

(
    "< NSLayoutConstraint:0x165c0280 V:[UIView:0x165bda30]-(44)-|   (Names: '|':UIView:0x166dd460 )>",
    "< NSAutoresizingMaskLayoutConstraint:0x165a7ce0 h=--& v=--& UIView:0x165bda30.midY == + 507>",
    "< NSAutoresizingMaskLayoutConstraint:0x165c50c0 h=--& v=--& V:[UIView:0x165bda30(34)]>",
    "< NSAutoresizingMaskLayoutConstraint:0x165c3a90 h=-&- v=-&- UIView:0x166dd460.height == UIWindow:0x1668f870.height - 20>",
    "< NSAutoresizingMaskLayoutConstraint:0x165a8070 h=--- v=--- V:[UIWindow:0x1668f870(568)]>"
)

Will attempt to recover by breaking constraint < NSLayoutConstraint:0x165c0280 V:[UIView:0x165bda30]-(44)-| (Names: '|':UIView:0x166dd460 )>

Break on objc_exception_throw to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.

This error only shows when the status bar is extended. There are no other constraints added programmatically, and the Interface Builder's VC for this class is empty. Everything is added programmatically.

This is driving me nuts!

1 Answer 1

3

You need to call setTranslatesAutoresizingMaskIntoConstraints: on the view with a parameter of NO. This will prevent the automatic addition of the NSAutoresizingMaskLayoutConstraints that you see in the log (so only your explicit constraints will apply).

Sign up to request clarification or add additional context in comments.

1 Comment

I did that, but the view then would not show at all. But when you said "only explicit constraints" I realized that then I should also have to specify height, width and origin as well. Now it's working as expected! Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.