Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何执行激活编程约束_Ios_Swift_Constraints - Fatal编程技术网

Ios 如何执行激活编程约束

Ios 如何执行激活编程约束,ios,swift,constraints,Ios,Swift,Constraints,我编写了一个函数,该函数定义UILabel并将其添加到UIView中,但是由于标签保留在屏幕的左上角,因此没有执行约束 代码如下: func timeAdded(screenHeight: CGFloat, screenWidth: CGFloat, viewController: UIView) { let readyLabel = UILabel() readyLabel.backgroundColor = UIColor.white readyLabel.tex

我编写了一个函数,该函数定义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

}
您可能需要的任何建议,敬请谅解

readyLabel.translatesAutoresizingMaskIntoConstraints = false 
NSLayoutConstraint.activate([
    readyLabel.centerXAnchor.constraint(equalTo: viewController.centerXAnchor)
    readyLabel.centerYAnchor.constraint(equalTo: viewController.centerYAnchor)
])
所以最好是读书