首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >标签约束无效

标签约束无效
EN

Stack Overflow用户
提问于 2017-03-30 10:45:33
回答 2查看 718关注 0票数 1
代码语言:javascript
复制
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))

    }


}

我的上述函数setUpViews()没有在view.Can上呈现我的标签--任何人都可以在这里帮助我。我没有收到任何错误,它编译很好,但没有输出。

addConstraints方法有什么问题吗?

请帮忙谢谢。

如果我添加了这个约束-:

代码语言:javascript
复制
addConstraintsWithFormat(format: "H:|[v0]|", views:titlelabel)

并注释这一行代码-:

代码语言:javascript
复制
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))

但这并不能表明我的观点,我需要的地方,以及我希望它如何发挥作用。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-30 12:09:09

好,就这样解决了-:

代码语言:javascript
复制
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))

但谁能详细解释一下这是怎么回事。这是谷歌搜索出的答案。

票数 1
EN

Stack Overflow用户

发布于 2017-03-30 11:11:34

您必须在setUpViews()方法中调用您的awakeFromNib()函数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43115600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档