Reputation: 3408
I am using AutoLayout in my sample app. I have three views, topView, middleView, bottomView.Following are the constraint I need,
topView:
middleView:
bottomView:
I want that based on device screen size, bottom view y origin should change so that middle and top view size will adjust. Problem here is there is no way to find out, what should be the y origin of bottom view and interface is providing permanent constraint like:
This is because there is no way to find out the height of views. Only difficulty is determining height for top and middle view.
Upvotes: 0
Views: 137
Reputation: 1873
You don't seem to care what the height of the top and middle view is, so I'm just going to make a decision for you: they will have the same height. Add the following contraints to the common superview of which these three views (_topView,_middleView and _bottomView) are subviews:
NSString *vfl = @"V:|-(10)-[topView]-(10)-[middleView]-(10)-[bottomView(==30)]-(10)-|";
NSDictionary *dict = @{@"topView":_topView,@"middleView":_middleView,@"bottomView":_bottomView};
NSArray *a = [NSLayoutConstraints
constraintsWithVisualFormat: vfl
options: 0
metrics: nil
views: dict];
Make sure you align them horizontally as well:
NSArray *b = [NSLayoutConstraints
constraintsWithVisualFormat: @"H:|-(10)-[topView]-(10)-|"
options: 0
metrics: nil
views: dict];
NSArray *c = [NSLayoutConstraints
constraintsWithVisualFormat: @"H:|-(10)-[middleView]-(10)-|"
options: 0
metrics: nil
views: dict];
NSArray *d = [NSLayoutConstraints
constraintsWithVisualFormat: @"H:|-(10)-[bottomView]-(10)-|"
options: 0
metrics: nil
views: dict];
Edit
The middle view will be a label, as you say. Labels have an intrinsic content size. If you don't set the height of this view, the autolayout system will know what to do instinctively. (Neat, right?) By pinning the top of _topView to the top of the superview and its bottom to the top of the label, its height should be automatically calculated. I have changed the code to reflect this.
Edit 2
In order to add constraints in code, find a common ancestor (superview) of these three views and write [superview addConstraints:a],[superview addConstraints:b], etc...
Make sure that autolayout in IB is turned off and that you set the translateResizingMasksToConstraints
to NO
.
Upvotes: 2