Ios 标签约束不起作用
我上面的函数setUpViews()没有在视图上呈现我的标签。有人能帮我吗。我没有收到任何错误,它编译得很好,但没有输出 添加约束方法是否存在任何问题 请帮忙,谢谢 如果我添加此约束-,它将起作用:Ios 标签约束不起作用,ios,swift,swift3,swift2,Ios,Swift,Swift3,Swift2,我上面的函数setUpViews()没有在视图上呈现我的标签。有人能帮我吗。我没有收到任何错误,它编译得很好,但没有输出 添加约束方法是否存在任何问题 请帮忙,谢谢 如果我添加此约束-,它将起作用: class videoCell:UICollectionViewCell { override init(frame: CGRect) { super.init(frame: frame) setUpViews() } let titlelabel :
class videoCell:UICollectionViewCell
{
override init(frame: CGRect) {
super.init(frame: frame)
setUpViews()
}
let titlelabel : UILabel =
{
var label = UILabel()
label.backgroundColor = UIColor.brown
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
func setUpViews()
{
addSubview(titlelabel)
addConstraint(NSLayoutConstraint(item: titlelabel, attribute:.top, relatedBy: .equal, toItem: thumbNailImageView, attribute: .bottom, multiplier: 1, constant: 8))
addConstraint(NSLayoutConstraint(item: titlelabel, attribute:.left, relatedBy: .equal, toItem: thumbNailImageView, attribute: .right, multiplier: 1, constant: 8))
addConstraint(NSLayoutConstraint(item: titlelabel, attribute: .right, relatedBy: .equal, toItem: thumbNailImageView, attribute: .right, multiplier: 1, constant: 0))
addConstraintsWithFormat(format: "V:[v0(20)]", views:titlelabel)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension UIView
{
func addConstraintsWithFormat(format:String,views:UIView...)
{
var allViews = [String:UIView]()
for data in 0...views.count-1
{
let key = "v\(data)"
allViews[key] = views[data]
}
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: allViews))
}
}
并注释这行代码-:
addConstraintsWithFormat(format: "H:|[v0]|", views:titlelabel)
但这并不能将我的视图固定在我需要的位置以及我希望它如何工作。您必须在awakeFromNib()方法中调用setUpViews()函数。好的,解决方法如下-:
addConstraint(NSLayoutConstraint(item: titlelabel, attribute:.left, relatedBy: .equal, toItem: thumbNailImageView, attribute: .right, multiplier: 1, constant: 8))
addConstraint(NSLayoutConstraint(item: titlelabel, attribute: .right, relatedBy: .equal, toItem: thumbNailImageView, attribute: .right, multiplier: 1, constant: 0))
但谁能详细解释一下这是怎么回事。这是谷歌搜索出来的答案。谢谢你的回复,但我在这个方法中有更多的视图,效果很好。我认为这是因为“左约束”的常量为
8
,“右约束”的常量为0。标签的左侧向右8点,而右侧在左侧。虽然我有点不喜欢,但这应该是原因。将“左约束”的常量更改为-8
,它应该会显示出来。@结果仍然相同。如果您注释掉“左约束”并将其替换为:NSLayoutConstraint(项:titleLabel,属性:。宽度,relatedBy:。相等,toItem:nil,属性:。notanaAttribute,乘数:1,常量:8),该怎么办
?
addConstraint(NSLayoutConstraint(item: titlelabel, attribute: .top, relatedBy: .equal, toItem: thumbNailImageView, attribute: .bottom, multiplier: 1.0, constant: 8))
addConstraint(NSLayoutConstraint(item: titlelabel, attribute: .leading, relatedBy: .equal, toItem: thumbNailImageView, attribute: .leading, multiplier: 4.0, constant: 8))
addConstraint(NSLayoutConstraint(item: titlelabel, attribute: .trailing, relatedBy: .equal, toItem: thumbNailImageView, attribute: .trailing, multiplier: 1.0, constant: 0))
addConstraint(NSLayoutConstraint(item: titlelabel, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 0, constant: 20))