繁体   English   中英

使用UILabel以编程方式进行自动布局不起作用

[英]Autolayout programmatically with UILabel doesn't work

因此,我想以编程方式在视图中将UILabel的水平和垂直居中。 我遵循此主题,但是它不起作用。 我怎样才能做到这一点?

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

将此添加到您的代码:

 noDataLabel.translatesAutoresizingMaskIntoConstraints = false

从苹果文档:

默认情况下,视图上的自动调整大小蒙版会产生完全确定视图位置的约束。 这允许自动布局系统跟踪其布局是手动控制的视图框架(例如,通过-setFrame :)。 当您通过添加自己的约束选择使用自动布局来放置视图时,必须将此属性设置为NO。 IB将为您做到这一点。

您还需要为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)
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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