我刚刚发现了 UIStackView,我正在尝试看看它是否最终可以使布局扩展内容变得更容易。
具体来说,我想做以下事情:
|-- UITextView ------------|
| some dynamic text here, | I want the text view's height to change
| it could be short, or it | depending on the height of the text
| could be tall | (set later, in code)
|-- UIView ----------------|
|xxxxxxxxxxxxxxxxxxxxxxxxxx| I want this view to expand to fill the
|xxxxxxxxxxxxxxxxxxxxxxxxxx| vertical space.
|xxxxxxxxxxxxxxxxxxxxxxxxxx|
|xxxxxxxxxxxxxxxxxxxxxxxxxx| It should start right below the
|xxxxxxxxxxxxxxxxxxxxxxxxxx| bottom of the UITextView
|xxxxxxxxxxxxxxxxxxxxxxxxxx|
|--------------------------|
我想我知道如何在常规约束下做到这一点。可以用 UIStackView 来做吗?我可能理解 UIStackView 的目的,但我希望能够更轻松地创建这样的布局。
当我将这两个 View 放入 UIStackView 中时,界面生成器会提示两个 View “需要约束:Y 位置或高度”。
最佳答案
以编程方式,它看起来像这样:
@IBOutlet private var stackView: UIStackView!
{
didSet {
stackView.addArrangedSubview(titleTextView)
stackView.addArrangedSubview(UIView())
}
}
lazy var titleTextView: UITextView =
{
let textView = UITextView()
textView.isScrollEnabled = false
return textView
}()
您不需要任何其他内容即可让 UITextView 自动展开。这不应该提示歧义。
关于ios - 将 UIStackView 与具有动态高度的 UITextView 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37166884/