我想在field1和field2之间添加一个填充,它在storyboard中工作,但是我需要以编程方式设置它,所以我尝试了这个,但是2个字段是彼此相邻的,没有这个间距两者之间30: 容器为绿色,field1为橙色,field2为蓝色:
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)
答案 0 :(得分:0)
你应该改变
str = "V:|[field1]-(==spacingV)-[field2]"
到
str = "V:|[field1]-(spacingV)-[field2]"