here situation, build of interface in interface builder auto layout, there subview complicated, rather layout custom code, find in documentation can override layoutsubviews() implement custom code
"subclasses can override method needed perform more precise layout of subviews. should override method if autoresizing , constraint-based behaviors of subviews not offer behavior want. can use implementation set frame rectangles of subviews directly."
but find when set frame of subviews, don't have effects, think there wrong interaction of auto layout system.but can tell me wrong?
when enable autolayout, code write set frames ignored ios system.
this how build ui these days:
-(void)viewdidload { [self initviews]; [self initconstraints]; } -(void)initviews { // -------------------------------------------- // default init method calls initwithframe: // method , passes cgrectzero // -------------------------------------------- self.textlabel = [[uilabel alloc] init]; self.textlabel.text = @"hello world!"; [self.view addsubview:self.textlabel]; } -(void)initconstraints { // line cause setframe: calls ignored self.textlabel.translatesautoresizingmaskintoconstraints = no; id views = @{ @"textlabel": self.textlabel }; // horizontal constraint [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|[textlabel]|" options:0 metrics:nil views:views]]; // vertical constraint [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|[textlabel]|" options:0 metrics:nil views:views]]; } if need change position of textlabel example, need instead declare nslayoutconstraint property in class:
@property (nonatomic, strong) nslayoutconstraint *textlabeltopconstraint; then modify constraint code this:
-(void)initconstraints { self.textlabel.translatesautoresizingmaskintoconstraints = no; id views = @{ @"textlabel": self.textlabel }; // horizontal constraint [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|[textlabel]|" options:0 metrics:nil views:views]]; // vertical constraint // -------------------------------------------- // note constant value, you'll update // later , tell ui relayout. // -------------------------------------------- self.textlabeltopconstraint = [nslayoutconstraint constraintwithitem:self.textlabel attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:self.view attribute:nslayoutattributetop multiplier:1.0 constant:0.0]]; // note theres no 's' after addconstraint [self.view addconstraint:self.textlabeltopconstraint]; } when tap button , want change textlabel's top position:
-(void)buttontappedmethod { // tell textlabel should offset 50 pixels top self.textlabeltopconstraint.constant = 50; // -------------------------------------------------------------------- // wrap layout command inside uiview animation block // see textlabel animate down rather instantly appear there // -------------------------------------------------------------------- [uiview animatewithduration:0.5 animations:^{ [self.view layoutifneeded]; }]; }
Comments
Post a Comment