我以编程方式创建了子视图,然后以编程方式向所述子视图添加了一个按钮。我使用锚点约束来设置子视图中按钮的位置,按钮现在显示在子视图上我想要的位置。但是,该按钮不可点击。我发现按钮的框架是(0.0,0.0,0.0,0.0),这可能是我的问题的原因。我的问题是,当帧为0时,按钮如何显示正常(但是不可点击)。我用来实例化和约束按钮的代码如下。谢谢你的帮助。
var confirmButton: UIButton = {
var button = UIButton()
button.layer.cornerRadius = 2.0
button.layer.masksToBounds = true
button.backgroundColor = UIColor(red: 41/255, green: 154/255, blue: 245/255, alpha: 1)
button.setTitle("Continue", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.isUserInteractionEnabled = true
button.isEnabled = true
return button
}()
promptView.addSubview(confirmButton)
confirmButton.addTarget(self, action: #selector(self.continuePressed), for: .touchUpInside)
confirmButton.leadingAnchor.constraint(equalTo: promptView.leadingAnchor, constant: 15).isActive = true
confirmButton.trailingAnchor.constraint(equalTo: promptView.trailingAnchor, constant: -15).isActive = true
confirmButton.bottomAnchor.constraint(equalTo: promptView.bottomAnchor, constant: -15).isActive = true
//confirmButton.topAnchor.constraint(equalTo: promptView.topAnchor, constant: -235)
confirmButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
答案 0 :(得分:-2)
你的代码几乎是正确的..我已经在我的项目中运行你的代码,一切正常......你可以看到我的代码和你一样......
var confirmButton: UIButton = {
var button = UIButton()
button.layer.cornerRadius = 2.0
button.layer.masksToBounds = true
button.backgroundColor = UIColor(red: 41/255, green: 154/255, blue: 245/255,
alpha: 1)
button.setTitle("Continue", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.isUserInteractionEnabled = true
button.isEnabled = true
return button
}()
var promptView : UIView = {
let viewPrompt = UIView()
viewPrompt.layer.cornerRadius = 2.0
viewPrompt.layer.masksToBounds = true
viewPrompt.backgroundColor = UIColor(red: 41/255, green: 154/255, blue: 245/255, alpha: 1)
return viewPrompt
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(promptView)
promptView.addSubview(confirmButton)
promptView.translatesAutoresizingMaskIntoConstraints = false
let centerX = NSLayoutConstraint(item: promptView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: 0.0)
view.addConstraint(centerX)
let centerY = NSLayoutConstraint(item: promptView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerY, multiplier: 1.0, constant: 0.0)
view.addConstraint(centerY)
let height = NSLayoutConstraint(item: promptView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 200)
promptView.addConstraint(height)
let width = NSLayoutConstraint(item: promptView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 200)
promptView.addConstraint(width)
confirmButton.addTarget(self, action: #selector(self.continuePressed(_:)), for: .touchUpInside)
confirmButton.leadingAnchor.constraint(equalTo: promptView.leadingAnchor, constant: 15).isActive = true
confirmButton.trailingAnchor.constraint(equalTo: promptView.trailingAnchor, constant: -15).isActive = true
confirmButton.bottomAnchor.constraint(equalTo: promptView.bottomAnchor, constant: -15).isActive = true
confirmButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
}
func continuePressed(_ sender : UIButton ){
print("Hello")
}
希望它对你有用!!!