我有一个标签,它从数据库中获取一些动态的数据。这些数据是字符串,有时可以是3-4-5行等。因此,这个标记位于一个UIView中。
--UIView
--Label如何使UIView具有一定的标签动态高度??
发布于 2016-09-09 12:03:23
你可以用故事板--这张照片--来做


将标签高度关系设置为大于或等于
并将视图高度关系设置为大于或等于。
它像魔法一样运作
发布于 2016-09-09 12:01:24
贝娄是解决你问题的有效方法。我用的是autoLayout。在testView中,您不设置heightAnchor
let testView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = UIColor.redColor()
return view
}()
let testLabel: UILabel = {
let label = UILabel()
label.numberOfLines = 0
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "jashfklhaslkfhaslkjdhflksadhflkasdhlkasdhflkadshkfdsjh"
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(testView)
testView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor).active = true
testView.centerYAnchor.constraintEqualToAnchor(view.centerYAnchor).active = true
testView.widthAnchor.constraintEqualToConstant(100).active = true
testView.addSubview(testLabel)
testLabel.topAnchor.constraintEqualToAnchor(testView.topAnchor, constant: 10).active = true
testLabel.leftAnchor.constraintEqualToAnchor(testView.leftAnchor, constant: 10).active = true
testLabel.bottomAnchor.constraintEqualToAnchor(testView.bottomAnchor, constant: -10).active = true
testLabel.rightAnchor.constraintEqualToAnchor(testView.rightAnchor, constant: -10).active = true
}发布于 2019-10-30 13:05:32
我知道这是迟了的回答,但这可能会对别人有帮助。
要使UIView的动态高度遵循故事板中的简单步骤






此技术与此UIView中的其他视图一起工作。问题是,您必须为这个UIView中的视图指定底部约束。
https://stackoverflow.com/questions/39410796
复制相似问题