Autolayout - Views on top of other views

Hello,


I am trying to do AutoLayout correctly. I often end up with views that look correct in Storyboard, look fine in Storyboard Preview, look fine one screen, but throw an Auto Layout Constraint error.


One case is when I put a view on top of another view. For example if I have a SEARCH BARwith 4 buttons below it, and I want the user to select one of the 4 buttons first, on top of the SEARCH BAR I put a LABEL with a colour background. Once a button is selected, I slide up the LABEL, and give focus to the SEARCH BAR. This throws an CONSTRAINT ERROR with Auto Layout when the App is run listing every single button and the search bar.


Another example is putting two views into a Main View if it contains a Segmented Control, and I want to design the views the Segment Control shows with Storyboard. I see no other way of doing this beside putting two subviews into the view, designing them with Storyboard. It works fine, but again throws a CONSTRAINT ERROR.


What are best practises with this ?


Thanks.

If you have constraints that you are adjusting at runtime, you can add enough constraints in the storyboard to keep IB happy but mark them as Placeholder (removed at build time). Then your code is responsible for adding sufficient constraints to position everything.


When you say "I slide up the LABEL". How are you doing that? Are you adding an additional constraint (that happens to conflict with some existing ones)? The proper way to do it, IMHO, is to either set the initial constraint from the storyboard to a non-required priority so when your extra one is added it takes precedence with no conflict, or make an outlet for the constraint you need to adjust, and set its constant property in code. I prefer the former since you don't usually end up having to hard code any dimensions.


You may need to post some code.

Autolayout - Views on top of other views
 
 
Q