ios - how to get auto layout to work in Xcode -


i looked http://developer.apple.com/library/ios/#recipes/xcode_help-interface_builder/articles/understandingautolayout.html explanations pretty vague.

i'm on ios 6. have view controller 2 child views, 1 on top of another:

viewcontroller layout

i want use autolayout set views in such way if change top view's frame programatically, bottom view resize accommodate top view's new frame. is, if increase top view's height, bottom view's height should drecrease keep same separation between both views; opposite should happen if decrease top view's height.

the default constraints these 2 views in xcode are:

tableview constraints

mapview constraints

and if hold command , resize top (map) view, bottom (tableview) seems correctly shrink or expand vertically honor space constraint between them.

however, minute try modifying top view's frame or bounds via code, instance:

self.topview.frame = cgrectmake(0, 0, 320, 514); 

the bottom view doesn't shrink. missing?

you can't modify self.topview.frame that. setting frame auto layout's job. need modify constraints , let auto layout update frame.

create outlet of type nslayoutconstraint in view controller:

@property (nonatomic, strong) iboutlet nslayoutconstraint *topviewheightconstraint; 

connect outlet height=127 constraint on top view in storyboard. see answer if need more connecting outlet constraint.

then, change height of top view, , have auto layout update frame of bottom view too, set constraint's constant:

self.topviewheightconstraint.constant = 514; 

at end of run loop (not right away, before screen updated), auto layout modify frames of both views satisfy changed constraint.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -