UIView child 自动边框大小
UIView child automatic frame size
我正在尝试将自定义 UIView (LabelIcon) 添加到 parent 视图,这不应该关心 child 的大小 - 他应该只是将它们以全宽堆叠在一起在 child 的高度上(会有所不同)。
目前这就是我在 parent 中添加 child 的方式:
let locationLabel = LabelIcon(frame: .zero, icon: "location", text: "Lane\nlane")
let locationLabel2 = LabelIcon(frame: .zero, icon: "location", text: "Second text line\nLine line line\nAnother line\nsome more line")
content.addSubview(locationLabel)
content.addSubview(locationLabel2)
(我不想在这里手动设置框架 - 应该是自动的)
这些视图的限制,因此它们至少在某种程度上是可见的(因为我没有设置框架):
locationLabel.snp.makeConstraints { (make) in
make.top.left.right.equalToSuperview()
make.height.equalTo(40)
}
locationLabel2.snp.makeConstraints { (make) in
make.left.right.equalToSuperview()
make.top.equalTo(locationLabel.snp.bottom)
make.height.equalTo(40)
}
这可行,但对每个元素都这样做会很痛苦。另外我现在不知道内部元素的高度。
我想要实现的是不必在 parent 中定义任何硬编码约束。 child 视图应自行决定它的高度。
child是这样定义的:
class LabelIcon: UIView {
init(frame: CGRect, icon: String, text: String) {
super.init(frame: frame)
self.backgroundColor = UIColor.orange
let iconImageView = UIImageView(image: UIImage(named: icon))
iconImageView.contentMode = UIViewContentMode.scaleAspectFit
self.addSubview(iconImageView)
let label = UILabel()
label.textAlignment = .left
label.numberOfLines = 0
label.text = text
self.addSubview(label)
iconImageView.snp.makeConstraints { (make) in
make.top.equalToSuperview().offset(5)
make.left.equalToSuperview().offset(10)
}
label.snp.makeConstraints { (make) in
make.top.equalToSuperview()
make.left.equalTo(iconImageView.snp.right).offset(20)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
如何定义 child 使其知道 parent 的宽度,但高度将根据标签大小自动完成?文本将具有动态行数。
在parent中会有很多这样的视图,因此我希望那里的约束尽可能少(不要为每个 LabelIcon 实例手动执行)。
基于这段代码,这是当前状态:
我所有的视图都是通过自动布局以编程方式完成的(使用 SnapKit,没有故事板或 xib)。我想使用 UIStackView,但无论如何 child 必须定义高度才能工作。
编辑:
使用这些约束时,视图本身是可见的:
locationLabel.snp.makeConstraints { (make) in
make.top.left.right.equalToSuperview()
}
locationLabel2.snp.makeConstraints { (make) in
make.left.right.equalToSuperview()
make.top.equalTo(locationLabel.snp.bottom)
}
Bu 意见相互重叠(标签 2 不在标签下方)。此外,视图的尺寸基本上为 0,0 - 没有橙色背景:
来自接受的答案:
标签约束:
label.snp.makeConstraints { (make) in
make.top.equalToSuperview()
make.left.equalTo(iconImageView.snp.right).offset(20)
make.bottom.equalToSuperview().offset(-10)
make.right.equalToSuperview().offset(-10)
}
Parent:
locationLabel.snp.makeConstraints { (make) in
make.top.left.equalToSuperview()
}
locationLabel2.snp.makeConstraints { (make) in
make.left.equalToSuperview()
make.top.equalTo(locationLabel.snp.bottom)
}
结果:
视图的内部标签像这样添加底部约束
make.bottom.equalToSuperview().offset(10)
//
添加右约束
make.right.equalToSuperview().offset(10)
删除任何静态高度
make.height.equalTo(40)
然后视图应根据文本调整大小,想法是通过静态值或左&&右值给标签宽度
我正在尝试将自定义 UIView (LabelIcon) 添加到 parent 视图,这不应该关心 child 的大小 - 他应该只是将它们以全宽堆叠在一起在 child 的高度上(会有所不同)。
目前这就是我在 parent 中添加 child 的方式:
let locationLabel = LabelIcon(frame: .zero, icon: "location", text: "Lane\nlane")
let locationLabel2 = LabelIcon(frame: .zero, icon: "location", text: "Second text line\nLine line line\nAnother line\nsome more line")
content.addSubview(locationLabel)
content.addSubview(locationLabel2)
(我不想在这里手动设置框架 - 应该是自动的)
这些视图的限制,因此它们至少在某种程度上是可见的(因为我没有设置框架):
locationLabel.snp.makeConstraints { (make) in
make.top.left.right.equalToSuperview()
make.height.equalTo(40)
}
locationLabel2.snp.makeConstraints { (make) in
make.left.right.equalToSuperview()
make.top.equalTo(locationLabel.snp.bottom)
make.height.equalTo(40)
}
这可行,但对每个元素都这样做会很痛苦。另外我现在不知道内部元素的高度。
我想要实现的是不必在 parent 中定义任何硬编码约束。 child 视图应自行决定它的高度。
child是这样定义的:
class LabelIcon: UIView {
init(frame: CGRect, icon: String, text: String) {
super.init(frame: frame)
self.backgroundColor = UIColor.orange
let iconImageView = UIImageView(image: UIImage(named: icon))
iconImageView.contentMode = UIViewContentMode.scaleAspectFit
self.addSubview(iconImageView)
let label = UILabel()
label.textAlignment = .left
label.numberOfLines = 0
label.text = text
self.addSubview(label)
iconImageView.snp.makeConstraints { (make) in
make.top.equalToSuperview().offset(5)
make.left.equalToSuperview().offset(10)
}
label.snp.makeConstraints { (make) in
make.top.equalToSuperview()
make.left.equalTo(iconImageView.snp.right).offset(20)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
如何定义 child 使其知道 parent 的宽度,但高度将根据标签大小自动完成?文本将具有动态行数。
在parent中会有很多这样的视图,因此我希望那里的约束尽可能少(不要为每个 LabelIcon 实例手动执行)。
基于这段代码,这是当前状态:
我所有的视图都是通过自动布局以编程方式完成的(使用 SnapKit,没有故事板或 xib)。我想使用 UIStackView,但无论如何 child 必须定义高度才能工作。
编辑: 使用这些约束时,视图本身是可见的:
locationLabel.snp.makeConstraints { (make) in
make.top.left.right.equalToSuperview()
}
locationLabel2.snp.makeConstraints { (make) in
make.left.right.equalToSuperview()
make.top.equalTo(locationLabel.snp.bottom)
}
Bu 意见相互重叠(标签 2 不在标签下方)。此外,视图的尺寸基本上为 0,0 - 没有橙色背景:
来自接受的答案:
标签约束:
label.snp.makeConstraints { (make) in
make.top.equalToSuperview()
make.left.equalTo(iconImageView.snp.right).offset(20)
make.bottom.equalToSuperview().offset(-10)
make.right.equalToSuperview().offset(-10)
}
Parent:
locationLabel.snp.makeConstraints { (make) in
make.top.left.equalToSuperview()
}
locationLabel2.snp.makeConstraints { (make) in
make.left.equalToSuperview()
make.top.equalTo(locationLabel.snp.bottom)
}
结果:
视图的内部标签像这样添加底部约束
make.bottom.equalToSuperview().offset(10)
//
添加右约束
make.right.equalToSuperview().offset(10)
删除任何静态高度
make.height.equalTo(40)
然后视图应根据文本调整大小,想法是通过静态值或左&&右值给标签宽度