ios - How to set the width of a control 50% of the screen? -
this simple thing driving me crazy. need 2 controls fill 50% percent of screen width each 1 (they side side) i'm unable observe how so.
i tried hundred of autolayout combination without success. please advice.
the next code auto layout create sure boxes taking 50% of parent view in width.
- (void)viewdidload { [super viewdidload]; uiview *view1 = [[uiview alloc] initwithframe:cgrectzero]; view1.translatesautoresizingmaskintoconstraints = no; view1.backgroundcolor = [uicolor greencolor]; [self.view addsubview:view1]; uiview *view2 = [[uiview alloc] initwithframe:cgrectzero]; view2.translatesautoresizingmaskintoconstraints = no; view2.backgroundcolor = [uicolor redcolor]; [self.view addsubview:view2]; nsdictionary *bindings = nsdictionaryofvariablebindings(view1, view2); [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"|[view1][view2]|" options:0 metrics:nil views:bindings]]; [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|[view1(200)]" options:0 metrics:nil views:bindings]]; [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|[view2(200)]" options:0 metrics:nil views:bindings]]; [self.view addconstraint:[nslayoutconstraint constraintwithitem:view1 attribute:nslayoutattributeright relatedby:nslayoutrelationequal toitem:self.view attribute:nslayoutattributecenterx multiplier:1.0 constant:0.0]]; [self.view addconstraint:[nslayoutconstraint constraintwithitem:view2 attribute:nslayoutattributeleft relatedby:nslayoutrelationequal toitem:self.view attribute:nslayoutattributecenterx multiplier:1.0 constant:0.0]]; }
the trick set left attribute of 2nd view (control) centerx of parent view , right attribute of 1st view (control) parent view centerx also
ios objective-c autolayout
No comments:
Post a Comment