【问题标题】:UIButton not responding to tapUIButton 没有响应点击
【发布时间】:2020-03-17 11:54:27
【问题描述】:

UIButton 点按时操作不起作用。我尝试了 Stack Overflow 上另一篇文章的其他建议,但没有任何效果。以下是我的代码:

    let saveBtn = GActionBtn(type: .system)

    lazy var footer: UIView = {
        let v = UIView()
        v.isUserInteractionEnabled = true
        v.addSubview(saveBtn)
        saveBtn.isUserInteractionEnabled = true
        saveBtn.addTarget(self, action: #selector(handleSave), for: .touchUpInside)
        saveBtn.backgroundColor = #colorLiteral(red: 0.4470588235, green: 0.6274509804, blue: 0.3960784314, alpha: 1)
        saveBtn.layer.cornerRadius = 27.5
        let attributeString = NSAttributedString(string: "Save".localized(), attributes: [NSAttributedString.Key.font: UIFont(name: "NunitoSans-Bold", size: 16), NSAttributedString.Key.foregroundColor: UIColor.white])
        saveBtn.setAttributedTitle(attributeString, for: .normal)
        NSLayoutConstraint.activate([
            saveBtn.centerXAnchor.constraint(equalTo: v.centerXAnchor),
            saveBtn.centerYAnchor.constraint(equalTo: v.centerYAnchor, constant: 54),
            saveBtn.widthAnchor.constraint(equalToConstant: 312),
            saveBtn.heightAnchor.constraint(equalToConstant: 52)
        ])
        return v
    }()

    @objc private func handleSave() {
        print("save")
    }

    private func setupTableView() {
        tableView = UITableView(frame: .zero, style: .grouped)
        tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 80, right: 0)
        tableView.separatorStyle = .none
        tableView.keyboardDismissMode = .interactive
        tableView.backgroundColor = .white
        tableView.tableFooterView = footer
    }

【问题讨论】:

  • 你应该使用tableview的默认footerView函数。然后尝试使用您的按钮

标签: ios swift uibutton


【解决方案1】:

@ferryawijayanto 根据Sh_Khan 的建议,是的,您应该将 UIButton 直接返回给tableFooterView。它会接受它。尝试如下,

 private func setupTableView() {
    tableView = UITableView(frame: .zero, style: .grouped)
    tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 80, right: 0)
    tableView.separatorStyle = .none
    tableView.keyboardDismissMode = .interactive
    tableView.backgroundColor = .white

    let saveBtn = UIButton (type: .system)
    saveBtn.isUserInteractionEnabled = true
    saveBtn.addTarget(self, action: #selector(handleSave), for: .touchUpInside)
    saveBtn.backgroundColor = #colorLiteral(red: 0.4470588235, green: 0.6274509804, blue: 0.3960784314, alpha: 1)
    saveBtn.layer.cornerRadius = 27.5
    let attributeString = NSAttributedString(string: "Save".localized(), attributes: [NSAttributedString.Key.font: UIFont(name: "NunitoSans-Bold", size: 16), NSAttributedString.Key.foregroundColor: UIColor.white])
    saveBtn.setAttributedTitle(attributeString, for: .normal)


    tableView.tableFooterView = saveBtn
}

如果你想要完整的宽度和高度,定义 heightForFooterSection 意味着 UIButton 会自动更新它的框架从页脚的框架。否则,您想使用静态宽度和高度进行自定义,您可以不受约束地使用框架。当然你也应该尝试约束。

我希望它有助于实现您想要的输出。

【讨论】:

  • 你是否从 UITableViewDataSource 设置 heightForFooterInSection 框架?否则,如果您在声明部分制作没有 saveBtn.frame = CGRectFrame 等约束的框架。
  • 当你提到它时,我已经尝试过了,因为我忘记了,但它仍然没有显示。不过我还没有尝试使用框架。
  • 嘿干得好@ferryawijayanto
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多