Reputation: 5525
I'm trying to learn how constraints work programmatically for iOS apps, by making a simple project (single view application) shown below:
I succeeded in creating the top space only using this code in viewDidLoad:
NSLayoutConstraint *myConstraint =[NSLayoutConstraint
constraintWithItem:_container
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:200];
[self.view addConstraint:myConstraint];
myConstraint =[NSLayoutConstraint
constraintWithItem:_container
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:216];
[_container addConstraint:myConstraint];
but I failed to set the height of _container
(UIView - subview of superview). How would I create multiple constraints in a case like this? Thank you.
UPDATE : I will manipulate the size of top-space programmatically so the _container
will move up and down, or even offscreen.
Upvotes: 2
Views: 1956
Reputation: 119272
You can set a height constraint in code using visual format language : "V:[view(==216.0)]"
or by using constraintWithItem:
where the second item is nil
, the second attribute is NSLayoutAttributeNotAnAttribute
and the constant value is your height.
Upvotes: 3