我有一个包含两个动态高度UILabel的UIView。我希望UIView适应UILabel的大小+一些填充。目前,我绘制标签,然后将UIView的宽度和高度约束设置为标签的组合大小。
目前我正在这样做:
func populateWithMessage(headline: String?, message: String) {
// Sets text
self.headerLabel.text = headline
self.messageLabel.text = message
self.headerLabel.sizeToFit()
self.messageLabel.sizeToFit()
self.layoutSubviews()
// Finding the label with the greatest width
var longestLabel: UILabel!
if self.headerLabel.frame.width > self.messageLabel.frame.width {
longestLabel = self.headerLabel
} else {
longestLabel = self.messageLabel
}
let combinedLabelHeight = self.headerLabel.frame.height + self.messageLabel.frame.height
// Update the container view's constraints
self.containerViewWidtConstraint.constant = longestLabel.frame.width + 10
self.containerViewHeightConstraint.constant = combinedLabelHeight + 10
self.updateConstraints()
}
不幸的是它不起作用。我正在使用Swift 2.3和Autolayout。