我想像这样添加UIButton:
let switchTheme:UIButton = { let button = UIButton.init() button.backgroundColor = .red button.setTitleColor(.blue,for:。normal) button.setTitle(…
您需要设置约束 activate state = true 。你可以做到这一点,
activate state = true
NSLayoutConstraint.activate([ //Move your existing code HERE with comma separated ])
如有任何问题,您可以检查以下功能:
func setConstraints() { NSLayoutConstraint.activate([ switchTheme.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor), // bottomAnchor to set bottom target. switchTheme.leftAnchor.constraint(equalTo: self.view.leftAnchor), // leftAnchor to set X left switchTheme.rightAnchor.constraint(equalTo: self.view.rightAnchor), // rightAnchor to set X right switchTheme.heightAnchor.constraint(equalToConstant: 40.0) //heightAnchor to set appropriate height. ]) }
您的约束必须激活:
switchTheme.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
你需要像这样简单地激活这些约束:
switchTheme.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true switchTheme.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true switchTheme.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true switchTheme.heightAnchor.constraint(equalToConstant: 40.0).isActive = true