ios - Get frame of a view after autolayout -


i have method:

- (void)underlinetextfield:(uitextfield *)tf {     cgfloat x = tf.frame.origin.x-8;     cgfloat y = tf.origin.y+tf.frame.size.height+1;     cgfloat width = self.inputview.frame.size.width-16;     uiview *line = [[uiview alloc] initwithframe:(cgrect){x,y,width,1}];     line.backgroundcolor = [uicolor whitecolor];     [self.inputview addsubview:line]; } 

that underlines input uitextfield; textfield has width changes depending on screen width (nib autolayout).

i have tried using

[self.view setneedslayout]; [self.view layoutifneeded]; 

and

[self.inputview setneedslayout]; [self.inputview layoutifneeded]; 

before call method no change in result. resulting line wider uitextfield (it matches original size in nib).

i want resulting frame of uitextfield in question after being processed autolayout

solution: (using 'masonry' autolayout)

- (uiview *)underlinetextfield:(uitextfield *)tf {     uiview *line = [[uiview alloc] initwithframe:cgrectzero];     line.backgroundcolor = [uicolor whitecolor];     [self.inputview addsubview:line];     [line mas_makeconstraints:^(masconstraintmaker *make) {         make.centerx.equalto(tf.mas_centerx);         make.width.equalto(tf.mas_width).with.offset(16);         make.height.equalto(@1);         make.top.equalto(tf.mas_bottom);     }];     return line; } 

your underline view has static frame, not connected textfield through constraints. instead of setting frame, add constraints self.inputview

- (void)underlinetextfield:(uitextfield *)tf {     uiview *line = [[uiview alloc] init];     line.backgroundcolor = [uicolor whitecolor];     [line settranslatesautoresizingmaskintoconstraints:no];     [self.inputview addsubview:line];      // vertical constraints     [self.inputview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:[line(==1)]-(-1)-|" options:0 metrics:nil views:@{ @"line": line}]];      // horizontal constraints     [self.inputview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|-(-8)-[line]-8-|" options:0 metrics:nil views:@{ @"line": line}]];      [self.inputview layoutifneeded]; } 

after layoutifneeded call, frame view should right. hoped got constants right. because line appears 1 unit under textview make sure unset clip subviews in storyboard textfield

i hope works you. let me know if have questions!


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -