标签限制不起作用

时间:2017-03-30 10:45:33

标签: ios swift swift3 swift2

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()未在视图上呈现我的标签。任何人都可以帮助我。我没有得到任何错误,它编译得很好,但没有输出。

addConstraints 方法是否存在任何问题。

请帮助谢谢。

如果我添加此约束,它会起作用 -

addConstraintsWithFormat(format: "H:|[v0]|", views:titlelabel)

并评论这行代码 - :

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))

但这并没有把我的观点固定在我需要的位置以及我希望它如何运作。

2 个答案:

答案 0 :(得分:1)

好的解决了这个 - :

<!DOCTYPE html>
<html>
    <head>
       <script src="https://unpkg.com/react@15/dist/react.min.js"></script>
       <script src="https://unpkg.com/react-dom@15/dist/react-dom.min.js"></script>
       <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.24.0/babel.js"></script>
    </head>
    <body>
       <div id="app"></div>
           <script type="text/babel">
              const HelloMessage = React.createClass({
                 render: function() {
                     return <div>Hello {this.props.name}</div>;
                 }
              });

              ReactDOM.render(<HelloMessage name="John" />, 
                  document.getElementById('app'));
          </script>
    </body>
</html>

但任何人都可以向我详细解释这是如何运作的。这是谷歌的答案。

答案 1 :(得分:0)

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