Ios 如何执行激活编程约束
我编写了一个函数,该函数定义UILabel并将其添加到UIView中,但是由于标签保留在屏幕的左上角,因此没有执行约束 代码如下: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
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)
])
所以最好是读书