【发布时间】:2016-08-03 10:59:25
【问题描述】:
所以我想以编程方式在我的视图中将 UILabel 水平和垂直居中。我关注this topic,但它不起作用。我该怎么做?
p/s:这是我的代码:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let noDataLabel = UILabel()
noDataLabel.text = "No data :("
noDataLabel.textColor = UIColor.redColor()
noDataLabel.font = UIFont.systemFontOfSize(20)
noDataLabel.sizeToFit()
self.view.addSubview(noDataLabel)
NSLayoutConstraint(item: noDataLabel, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1.0, constant: 0.0).active = true
NSLayoutConstraint(item: noDataLabel, attribute: .CenterY, relatedBy: .Equal, toItem: self.view, attribute: .CenterY, multiplier: 1.0, constant: 0.0).active = true
}
}
【问题讨论】:
标签: ios swift autolayout uilabel