[英]Issue with adding constraints to UIImage programmatically | Swift
How can I set up constraints for an image to sit on the top center directly above the titleLabel
of the following tableview extention. 如何为图像设置约束,使其位于
titleLabel
以下tableview扩展的titleLabel
上方的顶部中心。
The code below currently displays a titleLabel and a messageLabel right under it: 下面的代码当前在其下方显示一个titleLabel和一个messageLabel:
extension UITableView {
func setEmptyView(title: String, message: String) {
let emptyView = UIView(frame: CGRect(x: self.center.x, y: self.center.y, width: self.bounds.size.width, height: self.bounds.size.height))
let titleLabel = UILabel()
let messageLabel = UILabel()
titleLabel.translatesAutoresizingMaskIntoConstraints = false
messageLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.textColor = UIColor.black
titleLabel.font = UIFont(name: "HelveticaNeue-Bold", size: 25)
messageLabel.textColor = UIColor.lightGray
messageLabel.font = UIFont(name: "HelveticaNeue-Regular", size: 23)
emptyView.addSubview(titleLabel)
emptyView.addSubview(messageLabel)
titleLabel.centerYAnchor.constraint(equalTo: emptyView.centerYAnchor).isActive = true
titleLabel.centerXAnchor.constraint(equalTo: emptyView.centerXAnchor).isActive = true
messageLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 20).isActive = true
messageLabel.leftAnchor.constraint(equalTo: emptyView.leftAnchor, constant: 20).isActive = true
messageLabel.rightAnchor.constraint(equalTo: emptyView.rightAnchor, constant: -20).isActive = true
titleLabel.text = title
messageLabel.text = message
messageLabel.numberOfLines = 0
messageLabel.textAlignment = .center
// The only tricky part is here:
self.backgroundView = emptyView
self.separatorStyle = .none
}
func restore() {
self.backgroundView = nil
self.separatorStyle = .singleLine
}
}
I have tried adding the code below but it does not make it centered is off to the top left side: 我试过在下面添加代码,但不会使其居中显示在左上方:
let emptyImage = UIImage()
emptyImage = false
emptyView.addSubview(emptyImage)
emptyImage.centerXAnchor.constraint(equalTo: emptyView.centerXAnchor).isActive = true
emptyImage.bottomAnchor(equalTo: titleLabel.topAnchor, constant: 20).isActive = true
The image is a square of around 50x50 图片是大约50x50的正方形
You need to add a UIImageView
as a subview, and then constrain it to the title label. 您需要将
UIImageView
添加为子视图,然后将其约束到标题标签。
Give this a try - it will add an image view, centered above the title view, with a width of 50 and a height equal to its width (so, 50x50): 尝试一下-将在标题视图上方居中添加一个图像视图,其宽度为50,高度等于其宽度(因此为50x50):
extension UITableView {
func setEmptyView(title: String, message: String) {
let emptyView = UIView(frame: CGRect(x: self.center.x, y: self.center.y, width: self.bounds.size.width, height: self.bounds.size.height))
emptyView.backgroundColor = .cyan
let titleLabel = UILabel()
let messageLabel = UILabel()
titleLabel.translatesAutoresizingMaskIntoConstraints = false
messageLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.textColor = UIColor.black
titleLabel.font = UIFont(name: "HelveticaNeue-Bold", size: 25)
messageLabel.textColor = UIColor.lightGray
messageLabel.font = UIFont(name: "HelveticaNeue-Regular", size: 23)
emptyView.addSubview(titleLabel)
emptyView.addSubview(messageLabel)
titleLabel.centerYAnchor.constraint(equalTo: emptyView.centerYAnchor).isActive = true
titleLabel.centerXAnchor.constraint(equalTo: emptyView.centerXAnchor).isActive = true
messageLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 20).isActive = true
messageLabel.leftAnchor.constraint(equalTo: emptyView.leftAnchor, constant: 20).isActive = true
messageLabel.rightAnchor.constraint(equalTo: emptyView.rightAnchor, constant: -20).isActive = true
titleLabel.text = title
messageLabel.text = message
messageLabel.numberOfLines = 0
messageLabel.textAlignment = .center
// start of add image view code
let imgView = UIImageView()
imgView.translatesAutoresizingMaskIntoConstraints = false
if let img = UIImage(named: "s1") {
imgView.image = img
}
emptyView.addSubview(imgView)
NSLayoutConstraint.activate([
imgView.bottomAnchor.constraint(equalTo: titleLabel.topAnchor, constant: 0.0),
imgView.centerXAnchor.constraint(equalTo: titleLabel.centerXAnchor, constant: 0.0),
imgView.widthAnchor.constraint(equalToConstant: 50.0),
imgView.heightAnchor.constraint(equalTo: imgView.widthAnchor, multiplier: 1.0),
])
// end of add image view code
// The only tricky part is here:
self.backgroundView = emptyView
self.separatorStyle = .none
}
func restore() {
self.backgroundView = nil
self.separatorStyle = .singleLine
}
}
If you want the image view to have a little spacing above the title view, set the constant on this line (to a negative number): 如果您希望图像视图在标题视图上方有一点间距,请在此行上设置常量(为负数):
// this will add 20-pts vertical spacing between the
// bottom of the image view and the top of the title label
imgView.bottomAnchor.constraint(equalTo: titleLabel.topAnchor, constant: -20.0),
Edit If you want to adjust the vertical positioning, change this line: 编辑如果要调整垂直位置,请更改此行:
titleLabel.centerYAnchor.constraint(equalTo: emptyView.centerYAnchor).isActive = true
to this: 对此:
titleLabel.centerYAnchor.constraint(equalTo: emptyView.centerYAnchor, constant: 20).isActive = true
and set the constant
value until you're happy with it. 并设置
constant
值,直到满意为止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.