我有一个自定义UIView类,在其中创建UIView并将其放入UILabel。最初,我将UIView的高度设置为60.0。但是,如何使用UILabel调整UIView的高度?例如,如果UILabel包含5行文本,则UIView的高度也会增加到200pt(例如)。
这是我的课程:https://gist.github.com/orkhanalizade/747dc4fd1eb9f228ac964fb4048125dc
我尝试过
self.translatesAutoresizingMaskIntoConstraints = false
self.heightAnchor.constraint(greaterThanOrEqualToConstant: 60.0).isActive = true
NSLayoutConstraint(item: self, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 60.0).isActive = true
但这对我没有帮助 我做错了怎么解决?
答案 0 :(得分:0)
由于要调整常量的大小,因此可能要使用layoutIfNeeded()
。它将强制更新视图中的约束。您可以在Apple的documentation中了解更多相关信息。
答案 1 :(得分:0)
只需根据文本计算高度,并使用UIView.animate()方法更新UIView的高度约束。
答案 2 :(得分:0)
情侣笔记...
1)通常,视图不应设置自己的框架。如果您想将MyView
添加为另一个视图的子视图怎么办?根据需要设置其框架:
width: UIScreen.main.bounds.width - 16.0
不会给您想要的结果。
2)您不需要需要:
self.addConstraints(constraints)
3)我发现为元素提供不同的明显背景色很有帮助-轻松查看框架的作用。
以下是要点的编辑版本,以及用于添加/显示它的视图控制器:
import UIKit
class MyView: UIView {
var label: UILabel!
var text: String? {
didSet {
label.text = text
}
}
var cornerRadius: CGFloat = 0.0 {
didSet {
self.layer.cornerRadius = self.cornerRadius
self.layer.masksToBounds = true
}
}
var textColor: UIColor = UIColor.black {
didSet {
label.textColor = textColor
}
}
var isTextCentered: Bool = false {
didSet {
self.label.textAlignment = isTextCentered ? .center : .left
}
}
init() {
// we'll be using constraints, so no need to set a frame
super.init(frame: CGRect.zero)
initialize()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
fileprivate func initialize() {
label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.numberOfLines = 0
self.addSubview(label)
let constraints = [
NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 8.0),
NSLayoutConstraint(item: label, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1.0, constant: 8.0),
NSLayoutConstraint(item: label, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1.0, constant: -8.0),
NSLayoutConstraint(item: label, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: -8.0)
]
NSLayoutConstraint.activate(constraints)
// so we can see self's frame
self.backgroundColor = .red
// so we can see label's frame
label.backgroundColor = .yellow
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// instantiate the custom view
let v = MyView()
// we'll be using constraints
v.translatesAutoresizingMaskIntoConstraints = false
// add the view
view.addSubview(v)
NSLayoutConstraint.activate([
// constrain Top: 60 / Leading: 8 / Trailing: -8
v.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 60.0),
v.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 8.0),
v.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -8.0),
// constrain height >= 60
v.heightAnchor.constraint(greaterThanOrEqualToConstant: 60.0),
])
// add 10 lines of text
v.text = (1...10).map({ "Line \($0)" }).joined(separator: "\n")
}
}
结果: