简体   繁体   中英

Get frame of a view after autolayout

I have a 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 an input UITextField ; The textfield has a width that changes depending on the screen width (nib autolayout).

I have tried using

[self.view setNeedsLayout];
[self.view layoutIfNeeded];

and

[self.inputView setNeedsLayout];
[self.inputView layoutIfNeeded];

before I call this method with no change in result. the resulting line is much wider than the UITextField (it matches the original size in the Nib).

I just want the resulting frame of the UITextField in question after being processed by the 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 a static frame, it is not connected to the textField through constraints. Instead of setting the frame, add constraints to 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 the layoutIfNeeded call, the frame for your view should be right. I hoped I got the constants right. Because the line appears one unit under the textView make sure to unset Clip Subviews in the storyboard for the textField

I hope this works for you. Let me know if you have questions!

You could adjust the frame of the underline in viewDidLayoutSubviews which is called after auto layout has set all the frames.

Note that you're creating a new view and calling addSubview: each time. You should do that once when you create the text view, outside of this method. Otherwise you'll create a new UIView every time your user rotates the device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM