Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a problem with AutoLayout which is causing me a real headache. I have a scrollview & container view set up in a .xib using Autolayout. I need to dynamically add subviews to the container view and am trying to get them to play nice with Auto Layout. This is not going well...

Here is a picture of what I am trying to accomplish: enter image description here

So the Scroll View and Container Layer are set up in a xib already with autolayout and view 1-3 I am trying to add programatically. My first approach was:

[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[viewOne]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewOne)]]

which resulted in:

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:0x9d84de0 H:|-(20)-[UIView:0x9d922e0]   (Names: '|':UIView:0x9d91fb0 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x9d9fe90 h=--& v=--& UIView:0x9d922e0.midX ==>"
)

I have also tried using constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant but am struggling to get it to work the way I want. Am I doing something fundamentally wrong?

Stephen

share|improve this question
2  
how do you create viewOne? are you doing translatesAutoresizingMaskIntoConstraints = NO on viewOne? –  darren102 Dec 17 '13 at 17:02
 
Could you please provide us more info about your subviews? –  brianLikeApple Dec 17 '13 at 17:15
 
viewOne is created using [UIView alloc] init]. I have also tried [UIView alloc] initWithFrame:CGRectNull] –  Stephen Groom Dec 18 '13 at 11:49
 
The subviews will ultimately get more complex but for now they are just empty views. I have not yet tried translatesAutoResizingMaskIntoConstraints but will try now, ty –  Stephen Groom Dec 18 '13 at 11:50
add comment

1 Answer

up vote 0 down vote accepted
  1. Make sure all your subviews' translatesAutoresizingMaskIntoConstraints to NO.
  2. Make sure don't set frame to your subviews, because AutoLayout is talking about views' relationships not its frame.
  3. Make sure you have sufficient constraints that system is not confused.
share|improve this answer
1  
Thanks. The main problem was translatesAutoResizingMaskIntoConstraints and a little bit of carelessness when writing my visual format language. –  Stephen Groom Dec 18 '13 at 12:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.