关于UIView swift子类的NSLayoutConstraint

时间:2015-11-06 23:58:27

标签: ios swift uiview

我想在自己构建的自定义TableViewCell的UIView子类上为我的标签创建自动布局规则但不起作用

ModuleView.swift:

class ModuleView: UIView {


convenience init (initWithSubjectModules:[SubjectsModules], actualDataSource: FirstSemestrSubject, andFrame: CGRect ) {
    self.init(frame:CGRect.zero)

    let headerView = UIView(frame: CGRect(x: 40, y: 40, width: 400, height: 400))
    headerView.backgroundColor = UIColor.redColor()
    self.addSubview(headerView)

    let headerView1 = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    headerView1.backgroundColor = UIColor.yellowColor()
    headerView.addSubview(headerView1)

    let viewLabel = UILabel(frame: CGRectMake(0, 0 , 140 , 30))

    viewLabel.text = "hoho haha"
    viewLabel.textColor = UIColor.blackColor()

    headerView1.addSubview(viewLabel)

    let viewsDictionary = [ "headerView1": headerView1, "viewLabel": viewLabel ]
    let metrics = ["padding": 2 ]


    headerView1.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[viewLabel]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: metrics, views: viewsDictionary))
}

enter image description here

0 个答案:

没有答案