ios - In autolayout, how can I have view take up half of the screen, regardless of orientation? -
in portrait mode, have tableview on left-side, , webview on right-side. both take half of screen. have autolayout enabled.
but when rotate screen landscape mode, tableview's width takes less half screen's size, , webview ends taking more, instead of them beingness split.
why happen? how can have views take half screen in regard widths, regardless of screen orientation?
the first half of reply address case in want split view evenly between view (blue) , view b (red). sec half address case in want have view take half screen, view b not exist.
step 1:set blue's auto-layout constraints pictured. top, left, bottom of 0 superview. right of 0 reddish view.
step 2:set same, mirrored, constraints reddish view.
if you've completed first 2 steps correctly, should have auto-layout errors , warnings:
we need 1 more constraint prepare these errors/warnings , need.
step 3:hold control, click , drag 1 view other , select "equal widths". our views maintain same width. of our auto layout warnings , errors disappear, , our views half screen no matter orientation or device.
to add together these constraints in code using vfl, need next constraints:
@"h:|[blueview(==redview)][redview]|" @"v:|[blueview]|" @"v:|[redview]|" now, suppose case want single view take half screen, don't have view other half. can still auto layout, it's different set up. in example, our view blue, , parent view green.
step 1:this similar step 1 above, except don't add together right side constraint (this vary if want our view take different half).
step 2:like before, want assign "equal widths" constraint. in case, our view parent view. again, hold command , click drag 1 other.
at point, have auto layout warning. auto layout wants our frame match parent's width. clicking warning , choosing "update constraints" set hardcoded value in. don't want this.
step 3:select view , go size inspector. here, we'll able edit constraints.
click "edit" next "equal width to:" constraint. need alter multiplier value.
we need alter multiplier value 2.
the constraint changes "proportional width to:", , of our auto layout warnings , errors disappear. our view take half of super view.
to add together these constraints in code, can add together using vfl:
@"h:|[blueview]" @"v:|[blueview]|" but proportional width constraint can't added vfl. must add together such:
nslayoutconstraint *constraint = [nslayoutconstraint constraintwithitem:blueview attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem:superview attribute:nslayoutattributewidth multiplier:2.0 constant:0.0]; ios objective-c autolayout
No comments:
Post a Comment