【问题标题】:UITextView expands from topUITextView 从顶部展开
【发布时间】:2016-03-19 20:17:01
【问题描述】:

我正在尝试在textViewDidChange 中扩展UITextView

- (void)textViewDidChange:(UITextView *)textView
{
    int numLines = textView.contentSize.height/textView.font.lineHeight;
    if(numLines < 6)
    {
            CGRect frame = textView.frame;
            frame.size.height = textView.contentSize.height;
            textView.frame = frame;
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(int)textView.tag inSection:0];
            MessageDetailCell *cell = [tableMessageDetail cellForRowAtIndexPath:indexPath];
            cell.txtReplyHeightConstraint.constant = textView.frame.size.height;
            [cell updateConstraintsIfNeeded];
            [cell setNeedsLayout];
            [cell layoutIfNeeded];
    }
}   

这确实扩展了UITextView,但从顶部开始。我希望它从底部扩展。我在UITextView 的顶部和底部都应用了约束。

【问题讨论】:

  • 尝试更改优先级,使顶部约束的优先级高于底部约束。
  • @dasdom :这肯定会产生一些影响。我将顶部约束优先级设置为 1000,底部约束优先级设置为 250。现在 textView 上方的子视图也向上移动。 textView 仍然从顶部扩展。也许我在优先级方面做错了。

标签: ios iphone autolayout uitextview


【解决方案1】:

移除底部约束并添加宽度常量约束。它将从底部扩展。请试试这个。

【讨论】:

    【解决方案2】:

    我们可以用简单的逻辑来做到这一点。尝试以下方法,让我知道它是否有效。

    view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y - 10, view.frame.size.width, textView.contentSize.height + 10);
    

    在这里,我曾经将高度增加 10pt。您可以根据需要更改该值。

    【讨论】:

    • UITextView 被一个约束绑定。我正在使用自动布局。这不会有什么不同。
    • 这就是我正在尝试的。但 UITextView 从顶部展开。
    猜你喜欢
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    相关资源
    最近更新 更多