swift - 使用自动布局设置 UIView 高度以适合 UILabel subview

标签 swift uiview uilabel constraints subview

我尝试做什么

我有自定义 UIView,其中包含 UILabel 作为 subview 。我希望 UIView 包裹 UILabel 高度并根据文本长度设置其高度。将文本添加到 label.text 时,UILabel 会正确调整其高度。到目前为止一切顺利。

问题

UIView 不通过 UILabel 内容调整高度。文本应该位于下面的白色 UIView 内。这应该可以通过约束来解决,对吗?但我还是做不到。这可以纯粹通过 Storyboard来完成还是我必须在代码中手动设置 UIView 高度?

enter image description here

结果: enter image description here

最佳答案

尝试在 Interface Builder 中将 UILabel super View 的 Content Hugging 属性设置为 1000:

enter image description here

关于swift - 使用自动布局设置 UIView 高度以适合 UILabel subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38304855/

相关文章:

ios - 避免在从 `UITableView` 进行后台更新时跳入 `NSFetchedResultsController`

ios - WKWebView - iframe 内容未加载到模拟器/设备中

ios - 我无法在 Xcode 12 的 xib 中创建自定义 UIView

ios - 是否可以在旋转时自动调整 View 大小以使其尽可能大?

ios - Swift 3 - 如何在相机 View 上显示边框?

regex - 在 Swift 中使用模板的 NSRegularExpression

ios - 如何在 UIView subview 中应用自动布局 NIB 实例

ios - Swift:UITableViewCell 中的像素化文本

ios - UILabel 的 NSTextAlignmentCenter 不起作用

ios - 自定义 UITableViewCell - UILabel 未正确对齐