I\'m trying to use autolayout in my iOS app programmatically. I have a simple view controller with this init code
UIView *i
Just for the record: the exception concerning the missing superview gets already triggered by constraintsWithVisualFormat
not addConstraints
You have three problems:
First, you need to opt-in to layout based contraints on your both your button subviews
[button1 setTranslatesAutoresizingMaskIntoConstraints:NO];
[button2 setTranslatesAutoresizingMaskIntoConstraints:NO];
This will get rid of the NSAutoresizingMaskLayoutConstraint errors. Now you can get onto what's going on with your layout constraints.
Second, you have to addSubview
before adding the constraints. You are calling addContraints
on innerView before button1 and button2 are subviews of innerview.
Third, you should specify (although the docs indicate it is optional) whether the layout string is vertical or horizontal. @"H:|[button1][button2(==button1)]|"
. You will also want a vertical constraint like @"V:[button1]|"
which would align button1 to the bottom of innerview
and because you have aligned baselines on the buttons, button2 will follow.