【问题标题】:Swift 3 Programmatically Created Button Not ClickableSwift 3 以编程方式创建的按钮不可点击
【发布时间】:2017-10-14 22:43:01
【问题描述】:

我以编程方式创建了一个子视图,然后以编程方式向所述子视图添加了一个按钮。我使用锚约束来设置按钮在子视图中的位置,按钮现在出现在子视图上我想要的位置。但是,该按钮不可点击。我发现按钮的框架是 (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

【问题讨论】:

  • 确保将promptviewisUserInteractionEnabled属性设置为true
  • @Paulw11 有一个很好的建议。您在哪里找到按钮的 CGRect.zero 框架?很有可能它的帧尚未设置 - 尝试在控制器的 viewDidLayoutSubviews 函数中查找帧值。 (可见的 UIView 没有框架是没有意义的。)我以前遇到过这样的问题 - 花了一些时间,但我意识到 UISlider 在 UIButton 之类的代码中创建、可见并作为子视图添加没用。如果我记得的话,它与视图层次结构有关。
  • 没有运气设置 promptView 的 isUserInteractionEnabled。我直接在约束下打印确认按钮的框架
  • 我想我被奇怪的框架打印出来了。该按钮现在可以工作,但看起来不像被点击了。如何激活此功能?

标签: ios swift uibutton


【解决方案1】:

您的代码几乎是正确的..我已经在我的项目中运行了您的代码并且一切正常..您可以看到我的代码与您所做的相同..

    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")
    }

希望对你有用!!!

【讨论】:

  • 如果代码“几乎正确”,请说明错误的地方。
猜你喜欢
  • 2017-11-14
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-29
  • 2023-03-25
相关资源
最近更新 更多