iphone - How to adjust views using auto layout? -
i using autolayout in sample app. have 3 views, topview, middleview, bottomview.following constraint need,
topview:
- always start @ x origin 10.
- left , right margin 10.
- height should vary based on screen bounds(or superview).
middleview:
- there should 10 px vertical margin between top , middle view.
- left , right margin 10.
- height should vary based on screen bounds(or superview).
bottomview:
- there should 10 px vertical margin between middle , bottom view.
- left , right margin 10.
- height should constant, 30.
i want based on device screen size, bottom view y origin should change middle , top view size adjust. problem here there no way find out, should y origin of bottom view , interface providing permanent constraint like:
- top space superview middle view.
- top space superview bottom view.
this because there no way find out height of views. difficulty determining height top , middle view.
you don't seem care height of top , middle view is, i'm going make decision you: have same height. add following contraints common superview of these 3 views (_topview,_middleview , _bottomview) 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 align them horizontally 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 label, say. labels have intrinsic content size. if don't set height of view, autolayout system know instinctively. (neat, right?) pinning top of _topview top of superview , bottom top of label, height should automatically calculated. have changed code reflect this.
edit 2
in order add constraints in code, find common ancestor (superview) of these 3 views , write [superview addconstraints:a],[superview addconstraints:b], etc...
make sure autolayout in ib turned off , set translateresizingmaskstoconstraints
no
.
Comments
Post a Comment