Question

UITabBarController UITableView storyboard, constraints and auto layout. n Is there a method I can call in viewDidLoad to fix this?

Mavericks 10.9.2 Xcode 5.1

This is about an iOS app, UITabBarController containing UIViewController that has in its UIView

top area:UILabel
middle area:UITable
bottom area: Button, Label and Switch

Here is a photo from my first storyboard:

enter image description here

The top area has it Label constrained to the top, left and right of the UIView

The UITableView in the middle is constrained to the to a central area between top and bottom

The Button Label and Switch are constrained to the lower portion of the UIView, below the UITableView.

When the View first appears, the UITableView extended down too far and covers the Button, Label and Switch

https://www.dropbox.com/s/d0mqin7bh4cglgj/Screenshot%202014-03-26%2021.18.23.png

After the first rotational change the constraints work as intended.

https://www.dropbox.com/s/047fj86caqpp5kl/Screenshot%202014-03-26%2021.19.29.png

The constraints then continue to work through any number of orientation changes.

https://www.dropbox.com/s/fug0znnf8fc1etr/Screenshot%202014-03-26%2021.21.28.png

Is there any fix for this?

https://www.dropbox.com/s/0f1oabbj9e05j2h/Screenshot%202014-03-26%2021.22.47.png

Many Thanks, Mark

Was it helpful?

Solution

It seems you have ambiguous constraints, and so some parts of your interface (the "Email" button, the UISwitch, etc.) are only appearing some of the time. You need to add more constraints so there is no more ambiguity.

About Ambiguous Constraints

At launch time, and again each time you rotate the device and the interface rotates to compensate, layout occurs. This causes the position and size of your views to be recalculated based on constraints. If your layout works only some of the time, as you say - things are wrong when the app starts, but then they seem correct after you rotate (and layout is performed again) - that is usually a sign that you have ambiguous constraints. This means that your layout is underdetermined by the constraints you've provided. The runtime doesn't know what to do (it doesn't have enough information), and so it may do different things each time layout is freshly performed.

When you have ambiguous constraints, you don't get any notification in the console as the app runs. Thus, ambiguous constraints can escape your notice. This problem is particularly acute if your constraints are supplied in code. A useful trick is to pause your app in the debugger and type this at the console command line (and hit Return):

(lldb) po [[UIWindow keyWindow] _autolayoutTrace]

If any views have ambiguous layout, they will be clearly marked in the response from the runtime.

If your constraints are all formed in Interface Builder, Xcode 5 does a great job giving you warnings of the problem. Rule number one should be: if you see any warnings, pay attention! Work on your constraints until there are no more warnings in Interface Builder. Then, and only then, run the app.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top