简体   繁体   中英

Autolayout programmatically with UILabel doesn't work

So I want to center horizontal and vertical a UILabel in my View programmatically. I follow this topic but it doesn't work. How can I do that?

p/s: this is my code:

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
    }
}

Add this to your code:

 noDataLabel.translatesAutoresizingMaskIntoConstraints = false

From apple documentation:

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.

You also need create size constraints for uilabel like below and add it to your 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)

your implementation will look like

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)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM