動的な高さを持つ2つのUILabelを含むUIView
があります。私はUIView
をUILabelsのサイズといくつかのパディングに合わせたいと思っています。今私はラベルを描画して、UIViewの幅と高さの制約を組み合わせたラベルのサイズに設定します。
は今、私はこれをやっている: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を使用しています。
この答えはあなたの答えのための私は、その質問に伝えることができるものから便利http://stackoverflow.com/questions/16009405/uilabel-sizetofit-doesnt-work-with-autolayout-ios6 –
のおかげかもしれませんUILabelをUILabelに合わせる方法ではなく、テキストに合わせてUILabelを作成する方法に関するものです。 – NikMos
申し訳ありませんが、それは助けにはなりませんでした、私はすぐに自分自身を試して、答えを追加しました。 –