这是一个UIView,里面有一个UIImageView和一个UILabel。一些说明:
这是我的代码,您可以复制粘贴,但请确保为UIImageView的图像设置其他字符串:
class View2: UIView{
override init(frame: CGRect) {
super.init(frame: frame)
load()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
load()
}
func load(){
let overlappingView = UIView()
overlappingView.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(overlappingView)
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "0"
label.textColor = .white
label.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 751), for: .horizontal)
label.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 999), for: .vertical)
label.setContentHuggingPriority(UILayoutPriority(rawValue: 999), for: .vertical)
label.setContentHuggingPriority(UILayoutPriority(rawValue: 999), for: .horizontal)
let image = UIImageView()
image.image = UIImage(named: "Test")
image.translatesAutoresizingMaskIntoConstraints = false
image.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 1), for: .horizontal)
image.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 1), for: .vertical)
image.setContentHuggingPriority(UILayoutPriority(rawValue: 1), for: .vertical)
image.setContentHuggingPriority(UILayoutPriority(rawValue: 1), for: .horizontal)
overlappingView.addSubview(label)
overlappingView.addSubview(image)
overlappingView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
overlappingView.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: -2).isActive = true
overlappingView.widthAnchor.constraint(greaterThanOrEqualTo: self.widthAnchor, multiplier: 0.6).isActive = true
label.topAnchor.constraint(equalTo: overlappingView.topAnchor, constant: 5).isActive = true
label.bottomAnchor.constraint(equalTo: overlappingView.bottomAnchor, constant: -5).isActive = true
label.trailingAnchor.constraint(equalTo: overlappingView.trailingAnchor, constant: -5).isActive = true
label.heightAnchor.constraint(equalTo: image.heightAnchor, multiplier: 1).isActive = true
image.centerYAnchor.constraint(equalTo: overlappingView.centerYAnchor).isActive = true
image.leadingAnchor.constraint(equalTo: overlappingView.leadingAnchor, constant: 2).isActive = true
image.widthAnchor.constraint(equalTo: image.heightAnchor, multiplier: 1).isActive = true
image.trailingAnchor.constraint(equalTo: label.trailingAnchor, constant: -10).isActive = true
}
}
使用此代码,我看到图像以1:1的大小占据整个屏幕的高度。为什么它不尊重标签高度?我在界面构建器中有相同的约束,为什么它在代码中不起作用?
谢谢。
答案 0 :(得分:0)
您好我用您的代码玩了一下我修复它。我添加一些颜色,这样您就可以轻松看到差异。如果您有更多问题,我会再次帮助您。
class View2: UIView{
override init(frame: CGRect) {
super.init(frame: frame)
load()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
load()
}
func load(){
self.backgroundColor = .green
let overlappingView = UIView()
overlappingView.backgroundColor = .red
overlappingView.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(overlappingView)
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "0"
label.textColor = .black
let image = UIImageView(frame:CGRect(x: 10, y: 10, width: 10, height: 10))
image.image = UIImage(named: "btn_back")
image.tintColor = .black
image.backgroundColor = .blue
image.translatesAutoresizingMaskIntoConstraints = false
image.setContentCompressionResistancePriority(1, for: .horizontal)
overlappingView.addSubview(label)
overlappingView.addSubview(image)
label.sizeToFit()
overlappingView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
overlappingView.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: -2).isActive = true
overlappingView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1.0).isActive = true
overlappingView.heightAnchor.constraint(equalToConstant: label.frame.height + 10).isActive = true
label.topAnchor.constraint(equalTo: overlappingView.topAnchor, constant: 5).isActive = true
label.bottomAnchor.constraint(equalTo: overlappingView.bottomAnchor, constant: -5).isActive = true
label.trailingAnchor.constraint(equalTo: overlappingView.trailingAnchor, constant: -5).isActive = true
label.heightAnchor.constraint(equalTo: image.heightAnchor, multiplier: 1).isActive = true
image.centerYAnchor.constraint(equalTo: overlappingView.centerYAnchor).isActive = true
image.leadingAnchor.constraint(equalTo: overlappingView.leadingAnchor, constant: 2).isActive = true
image.widthAnchor.constraint(equalTo: image.heightAnchor, multiplier: 1).isActive = true
image.trailingAnchor.constraint(equalTo: label.leadingAnchor, constant: -10).isActive = true
}
}