How to resize two subviews based on priorities using Cocoa Autolayout? -
i playing autolayout in cocoa , things not clear me.
i have 2 views on window. width of each view 1/2 width of parent window.
| | | | | | | view1 | view2 | | | | | | |
if resize window want view2 resize first.
| | | | | | | view1 |view2| | | | | | |
when view2 reaches minimal size want view1 resized minimal size.
| | | | | | |view1|view2| | | | | | |
how can that?
the layout seems bit unspecified. when view2 start shrinking instead of matching view1's size? assuming views should same size until view1 reaches soft minimum. @ point, view2 resizes until reaches minimum, , view1 resizes until reaches minimum.
we can have behavior adding priorities contraints. in order of importance have:
- view1 , view2 >= minimum
- view1 >= view1softminimum
- view1 == view2
contraint 1 must above window resizing priority. can make required (which default).
constraint 2 must above contraint 3, below nslayoutprioritydragthatcannotresizewindow. make 480.
contraint 3 must below contraint 2, make 479.
we can express of contraints in 1 visual format string, can add
|[view1(>=view1minimum,>=view1softminimum@480,==view2@479)][view2(>=view2minimum)]|
here code tested with:
nsview *view1 = [[nstextview alloc] initwithframe:nszerorect]; nsview *view2 = [[nstextview alloc] initwithframe:nszerorect]; [view1 settranslatesautoresizingmaskintoconstraints:no]; [view2 settranslatesautoresizingmaskintoconstraints:no]; nsview *contentview = [self.window contentview]; [contentview addsubview:view1]; [contentview addsubview:view2]; nsdictionary *viewsdictionary = nsdictionaryofvariablebindings(view1, view2); [contentview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|[view1]|" options:nslayoutconstraintorientationvertical metrics:null views:viewsdictionary]]; [contentview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|[view2]|" options:nslayoutconstraintorientationvertical metrics:null views:viewsdictionary]]; nsdictionary *metrics = [nsdictionary dictionarywithobjectsandkeys: [nsnumber numberwithfloat:300], @"view1softminimum", [nsnumber numberwithfloat:150], @"view1minimum", [nsnumber numberwithfloat:150], @"view2minimum", nil]; [contentview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"|[view1(>=view1minimum,>=view1softminimum@480,==view2@479)]-[view2(>=view2minimum)]|" options:0 metrics:metrics views:viewsdictionary]];
Comments
Post a Comment