【问题标题】:Autolayout programmatically with UILabel doesn't work使用 UILabel 以编程方式自动布局不起作用
【发布时间】: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


    【解决方案1】:

    将此添加到您的代码中:

     noDataLabel.translatesAutoresizingMaskIntoConstraints = false
    

    来自苹果文档:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多