使用 UILabel 以编程方式自动布局不起作用
Autolayout programmatically with UILabel doesn't work
所以我想以编程方式在我的视图中将 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
}
}
您还需要为 uilabel
创建大小限制,如下所示,并将其添加到您的 superView
NSLayoutConstraint(item: noDataLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100)
NSLayoutConstraint(item: noDataLabel, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 40)
您的实施将类似于
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()
noDataLabel.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(noDataLabel)
let horizontal = NSLayoutConstraint(item: noDataLabel, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1.0, constant: 0.0).active = true
let vertical = NSLayoutConstraint(item: noDataLabel, attribute: .CenterY, relatedBy: .Equal, toItem: self.view, attribute: .CenterY, multiplier: 1.0, constant: 0.0).active = true
let width = NSLayoutConstraint(item: noDataLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100)
let height = NSLayoutConstraint(item: noDataLabel, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 40)
self.view.addConstraint(horizontal)
self.view.addConstraint(vertical)
self.view.addConstraint(width)
self.view.addConstraint(height)
}
}
将此添加到您的代码中:
noDataLabel.translatesAutoresizingMaskIntoConstraints = false
来自苹果文档:
By default, the autoresizing mask on a view gives rise to
constraints that fully determine
the view's position. This allows the auto layout system to track the frames of views whose
layout is controlled manually (through -setFrame:, for example).
When you elect to position the view using auto layout by adding your own constraints,
you must set this property to NO. IB will do this for you.
所以我想以编程方式在我的视图中将 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
}
}
您还需要为 uilabel
创建大小限制,如下所示,并将其添加到您的 superView
NSLayoutConstraint(item: noDataLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100)
NSLayoutConstraint(item: noDataLabel, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 40)
您的实施将类似于
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()
noDataLabel.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(noDataLabel)
let horizontal = NSLayoutConstraint(item: noDataLabel, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1.0, constant: 0.0).active = true
let vertical = NSLayoutConstraint(item: noDataLabel, attribute: .CenterY, relatedBy: .Equal, toItem: self.view, attribute: .CenterY, multiplier: 1.0, constant: 0.0).active = true
let width = NSLayoutConstraint(item: noDataLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100)
let height = NSLayoutConstraint(item: noDataLabel, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 40)
self.view.addConstraint(horizontal)
self.view.addConstraint(vertical)
self.view.addConstraint(width)
self.view.addConstraint(height)
}
}
将此添加到您的代码中:
noDataLabel.translatesAutoresizingMaskIntoConstraints = false
来自苹果文档:
By default, the autoresizing mask on a view gives rise to constraints that fully determine the view's position. This allows the auto layout system to track the frames of views whose layout is controlled manually (through -setFrame:, for example). When you elect to position the view using auto layout by adding your own constraints, you must set this property to NO. IB will do this for you.