`

UITextView autolayout 高度自适应

    博客分类:
  • ios
 
阅读更多
UITextView *t = [[UITextView alloc] initWithFrame:CGRectMake(20, 0, [UIScreen mainScreen].bounds.size.width - 40, 100)];
    t.backgroundColor = [UIColor yellowColor];
    t.text = @"11111111111111的方法的事发生的事的方法的事发生的事的方法的事发生的事的方法的事发生的事的方法的事发生的事的方法的事发生的事的方法的事发生的事的方法的事发生的事的方法的事发生的事的方法的事发生的事的方法的事发生的事的方法的事发生的事的方法的事发生的事的方法的事发生的事的方法的事发生的事的方法的事发生的事的方法的事发生的事的方法的事发生的事";
    t.translatesAutoresizingMaskIntoConstraints = NO;
    [v addSubview:t];
    
    [v addConstraint:[NSLayoutConstraint constraintWithItem:t attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:v attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
    [v addConstraint:[NSLayoutConstraint constraintWithItem:t attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:v attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
    [v addConstraint:[NSLayoutConstraint constraintWithItem:t attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:v attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    NSLayoutConstraint *c = [NSLayoutConstraint constraintWithItem:t attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:t.contentSize.height];
    [t addConstraint:c];
    CGSize sizeThatFitsTextView = [t sizeThatFits:CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, MAXFLOAT)];
    
    c.constant = sizeThatFitsTextView.height;

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics