簡體   English   中英

UIButton 沒有響應點擊

[英]UIButton not responding to tap

當我點擊時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
    }

@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 會自動從頁腳的框架更新它的框架。 否則,您想自定義靜態寬度和高度,您可以不受限制地使用框架。 當然,您也應該嘗試使用約束。

我希望它有助於實現您想要的輸出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM