间距填充在两个视图之间不起作用,具有可视布局约束?

时间:2015-02-19 15:29:57

标签: ios constraints visual-format-language

我想在field1和field2之间添加一个填充,它在storyboard中工作,但是我需要以编程方式设置它,所以我尝试了这个,但是2个字段是彼此相邻的,没有这个间距两者之间30: 容器为绿色,field1为橙色,field2为蓝色:

enter image description here

    var fviews = ["field1":field1, "field2":field2, "field3":field3, "field4":field4 ]
    let metrics : [NSObject: AnyObject] = ["spacingV" : 30]

    //FIELD 2 
    fviews = [ "field1":field1, "field2":field2 ]
    str = "V:|[field1]-(==spacingV)-[field2]" //----> HERE, I also tried -spacingV-
    var verticalFields = NSLayoutConstraint.constraintsWithVisualFormat(str, options: nil, metrics: metrics, views: fviews)
    self.view.addConstraints(verticalFields)

    //equal height with multiplier 0.2 :
    equalHeightMult = NSLayoutConstraint(item: field2,
        attribute: NSLayoutAttribute.Height,
        relatedBy: NSLayoutRelation.Equal,
        toItem: container,
        attribute: NSLayoutAttribute.Height,
        multiplier: 0.2,
        constant: 0)
    self.view.addConstraint(equalHeightMult)

    //align X center
    centerXConstraint =
        NSLayoutConstraint(item: field2,
            attribute: NSLayoutAttribute.CenterX,
            relatedBy: NSLayoutRelation.Equal,
            toItem: container,
            attribute: NSLayoutAttribute.CenterX,
            multiplier: 1.0,
            constant: 0)
    self.view.addConstraint(centerXConstraint)

    //HORIZONTAL: equal width with multiplier 0.7 :
    equalWidthMult = NSLayoutConstraint(item: field2,
        attribute: NSLayoutAttribute.Width,
        relatedBy: NSLayoutRelation.Equal,
        toItem: container,
        attribute: NSLayoutAttribute.Width,
        multiplier: 0.7,
        constant: 0)
    self.view.addConstraint(equalWidthMult)

1 个答案:

答案 0 :(得分:0)

你应该改变

str = "V:|[field1]-(==spacingV)-[field2]"

str = "V:|[field1]-(spacingV)-[field2]"