【问题标题】:How to execute activate programmatic constraints如何执行激活程序约束
【发布时间】:2018-12-30 21:54:15
【问题描述】:

我已经编写了一个函数,它定义并添加了一个 UILabel 到 UIView - 但是约束没有被执行,因为标签仍然在屏幕的左上角。

代码如下:

func timeAdded(screenHeight: CGFloat, screenWidth: CGFloat, viewController: UIView) {


    let readyLabel = UILabel()
    readyLabel.backgroundColor = UIColor.white
    readyLabel.textAlignment = .center
    readyLabel.text = "00:00"
    readyLabel.textColor = UIColor.blue
    //readyLabel.alpha = 0
    readyLabel.font = UIFont(name: "panl-font-4", size: 60)
    readyLabel.sizeToFit()

    viewController.addSubview(readyLabel)

    readyLabel.centerXAnchor.constraint(equalTo: viewController.centerXAnchor, constant: 0).isActive = true
    readyLabel.centerYAnchor.constraint(equalTo: viewController.centerYAnchor, constant: 0).isActive = true

}

任何建议表示赞赏

【问题讨论】:

    标签: ios swift constraints


    【解决方案1】:

    你可能需要

    readyLabel.translatesAutoresizingMaskIntoConstraints = false 
    NSLayoutConstraint.activate([
        readyLabel.centerXAnchor.constraint(equalTo: viewController.centerXAnchor)
        readyLabel.centerYAnchor.constraint(equalTo: viewController.centerYAnchor)
    ])
    

    所以最好阅读This

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 2021-06-27
      • 1970-01-01
      • 2020-03-02
      相关资源
      最近更新 更多