[英]Padding around image programmatically
I need to create a component which displays an image with a label in swift. 我需要创建一个组件来快速显示带有标签的图像。
I created a class which extends UIStackView
and placed an ImageView
and a UILabel
and it works. 我创建了一个扩展
UIStackView
的类,并放置了ImageView
和UILabel
并且可以正常工作。
Problem : the result is ugly, I need to add some padding around the UIImage
. 问题 :结果很难看,我需要在
UIImage
周围添加一些填充。 I tried to include the image in a UIView
that is bigger than the UIImage
but it make the image disappear! 我试图将图像包含在比
UIImage
大的UIView
,但它会使图像消失! At runtime, this warning message is displayed : 在运行时,显示以下警告消息:
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x60000009e820 h=--& v=--& UIImageView:0x7fea7fc02850.width == 0 (active)>",
"<NSLayoutConstraint:0x60400009b4e0 UIImageView:0x7fea7fc02850.width == 40 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60400009b4e0 UIImageView:0x7fea7fc02850.width == 40 (active)>
So, for a reason I don't understand, the image automatically gets a constraint width = 0
which discards the constraint width = 40
因此,由于我不了解的原因,图像会自动获得约束
width = 0
,这会丢弃约束width = 40
Here is the code: 这是代码:
class HeaderView: UIStackView {
var label: UILabel = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
}
required init(coder: NSCoder) {
super.init(coder: coder)
}
func setup(title: String, image: UIImage?) {
axis = UILayoutConstraintAxis.horizontal
let imgView = UIImageView()
imgView.image = image!
imgView.widthAnchor.constraint(equalToConstant: 40).isActive = true
imgView.heightAnchor.constraint(equalToConstant: 40).isActive = true
let paddedView = UIView()
paddedView.addSubview(imgView)
paddedView.widthAnchor.constraint(equalToConstant: 50).isActive = true
paddedView.heightAnchor.constraint(equalToConstant: 50).isActive = true
addArrangedSubview(paddedView)
label.text = title
addArrangedSubview(label)
}
}
translatesAutoresizingMaskIntoConstraints
to false to avoid conflicts with Auto Layout ones. translatesAutoresizingMaskIntoConstraints
设置为false,以避免与自动布局的冲突。 imgView
in paddedView
is ambiguous. imgView
在paddedView
位置不明确。 It would be better to constraint edges of imgView
to paddedView
instead of direct setting imgView
size. imgView
到paddedView
,而不是直接设置imgView
大小。 .fill
by default. .fill
。 Here is the code 这是代码
func setup(title: String, image: UIImage?) {
axis = .vertical
alignment = .center
label.text = title
let imageView = UIImageView(image: image)
imageView.translatesAutoresizingMaskIntoConstraints = false
let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(imageView)
NSLayoutConstraint.activate([
imageView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 10.0),
imageView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 10.0),
imageView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -10.0),
imageView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -10.0),
containerView.heightAnchor.constraint(equalToConstant: 50.0),
containerView.widthAnchor.constraint(equalToConstant: 50.0)
])
stackView.addArrangedSubview(containerView)
stackView.addArrangedSubview(label)
}
Set imgView , paddedView and label translatesAutoresizingMaskIntoConstraints property to false 将imgView,paddedView和labels的translatesAutoresizingMaskIntoConstraints属性设置为false
--- Aslo -阿斯洛
give the imageView top , bottomm , leading and trailing constraints after lowering priority of width and height 降低宽度和高度的优先级后,给imageView top,bottomm,前导和尾随约束
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.