动态自动布局调整大小

时间:2018-06-15 14:02:22

标签: ios swift autolayout swift4

我的容器内有UITextView。容器应根据文本视图的高度调整大小。我目前正在使用那些线来使它工作

 let fixedWidth = textView.frame.size.width
    let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
    let textViewHeight = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)

    containerView.frame.size = textViewHeight

问题是,我正在使用其他约束的自动布局,我也希望将其作为自动布局进行管理。 我尝试在我的自动布局中使用.greatestFiniteMagnitude,但这对我没什么用。

1 个答案:

答案 0 :(得分:1)

以下是允许文本字段自动调整大小并自动展开其包含视图的布局和约束。

containerView(蓝色)受限于Top / Leading / Trailing,但 底部或高度。

textView受限于Top / Leading / Trailing / Bottom,全部位于8,滚动已禁用。

少量文字: enter image description here

更长的文字数量: enter image description here

在运行时,当您在字段中键入时设置文本 - - 其高度将根据文本量自动调整大小。