简体   繁体   中英

resizing UITextView doesn't obey constraint

#define MAXHEIGHT 100

...

- (void)textViewDidChange:(UITextView *)textView
{

    CGFloat fixedWidth = textView.frame.size.width;
    CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXHEIGHT)];
    CGRect newFrame = textView.frame;
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
    textView.frame = newFrame;
}

I'm trying to resize a UITextView based on the text input. I want to constrain the height of the textview to 100. Everything about this solution works except that it doesn't constrain the height to 100 (ie it will continuing expanding in height when the user continually adds text?

您是否尝试过textView.frame = CGRectMake(x,y,width,100);

A UITextView is meant for multiple lines, where a UITextField is meant for a single line of text. Try using the delegate method -(void)textFieldDidChange instead.

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