如何在Swift中添加边距

时间:2016-05-15 23:38:11

标签: ios swift2

我似乎无法向我的UILabel添加任何边距,并将其包装在堆栈视图中也无济于事。最好我可以告诉.layoutMargins行完全被忽略了......

let helloLabel:UILabel = UILabel()
helloLabel.backgroundColor = UIColor.purpleColor()
helloLabel.text = "Hello, World!"
helloLabel.textAlignment = .Center
helloLabel.layoutMargins = UIEdgeInsetsMake(25,80,20,10)
stack.addArragnedSubView(helloLabel)

1 个答案:

答案 0 :(得分:-1)

显然这是不可能做到的。但是,如果你可以接近顶部和底部边距相同的话,你可以接近......

let helloLabel:UILabel = UILabel()
helloLabel.backgroundColor = UIColor.purpleColor()
helloLabel.text = "Hello, World!"
helloLabel.textAlignment = .Center
stack.addArragnedSubView(helloLabel)

NSLayoutConstraint(item:stack, attribute:.Width, relatedBy:.Equal, toItem:self.view, attribute:.Width, multiplier:1, constant:0).active = true
//top margin
stack.spacing = 25
//left margin
NSLayoutConstraint(item:helloLabel, attribute:.Leading, relatedBy:.Equal, toItem:stack, attribute:.Leading, multiplier:1, constant:80).active = true
//right margin
NSLayoutConstraint(item:helloLabel, attribute:.Trailing, relatedBy:.Equal, toItem:stack, attribute:.Trailing, multiplier:1, constant:-10).active = true