[英]Swift | How to add Trailing Space to UILabel programmatically?
I want to add to a UILabel
a Trailing space programmatically. 我想以编程方式向UILabel
添加尾随空间。 At the moment I'm trying this: 目前,我正在尝试这样做:
let trailingMargin = NSLayoutConstraint(item: myUILabel, attribute: NSLayoutAttribute.TrailingMargin,
relatedBy: NSLayoutRelation.Equal, toItem: view,
attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 20)
myUILabel.addConstraint( trailingMargin )
But my app always crashes. 但是我的应用程序总是崩溃。
myUILabel
is in a view and I want that it has a: myUILabel
在视图中,我希望它具有:
Trailing Space to: Superview
Equals: 20
Any advice? 有什么建议吗?
Use it this way: 使用这种方式:
override func viewDidLoad() {
super.viewDidLoad()
let trailingMargin = NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Trailing,
relatedBy: NSLayoutRelation.Equal, toItem: myUILabel,
attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 20)
myUILabel.setTranslatesAutoresizingMaskIntoConstraints(false)
NSLayoutConstraint.activateConstraints([trailingMargin])
}
Although your question is slightly confusing would not this work for you? 尽管您的问题有点令人困惑,但这对您来说行不通吗?
func addSpace(inout myLabel : UILabel) {
self.myLabel.text = "\(self.myLabel.text) "
}
FYI: inout
means you are passing by reference - thus the variable passed in can be modified by the function 仅供参考: inout
表示您正在通过引用传递-因此传入的变量可以由函数修改
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.