簡體   English   中英

tableFooterView中的UIButton未注冊觸摸

[英]UIButton in tableFooterView not registering touches

我有一個UITableView並將自定義頁腳設置為tableFooterView而不是 sectionFooter )。 我這樣做是這樣的:

override init(frame: CGRect) {
    super.init(frame: frame)
    setupViews()
    MyTableView.tableFooterView = tableFooter 
    MyTableView.tableFooterView?.addSubview(nextButton)
}

var tableFooter: UIView = {
    let tableFooter = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 64)
    tableFooter.backgroundColor = .white
    tableFooter.isUserInteractionEnabled = true
    return tableFooter
}()

var nextButton: UIButton = {
    let nextButton = UIButton(frame: CGRect(x: normalSpacing, y: 0, width: UIScreen.main.bounds.width - 32, height: 64)
    nextButton.backgroundColor = UIColor.green
    nextButton.setTitle("Next", for: .normal)
    nextButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 12)
    nextButton.setTitleColor(UIColor.white, for: .normal)
    nextButton.setTitleColor(UIColor.white.withAlphaComponent(0.75), for: .selected)
    nextButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    nextButton.layer.cornerRadius = 20
    nextButton.isUserInteractionEnabled = true
    nextButton.addTarget(self, action: #selector(nextAction), for: .touchUpInside)
    return nextButton
}()

@objc func nextAction() {
    print("Next tapped")
}

現在,該按鈕可見,並顯示在我希望該按鈕所在的位置。 我還可以設置背景顏色,它會出現在按鈕的后面,因此,我認為框架不是這里的問題(因為與此相關的問題很多)。 如果我錯了糾正我。 我認為需要啟用的所有地方都啟用了用戶交互。

關於如何解決此問題的任何想法? 我已經嘗試了幾乎所有內容,但似乎無法解決。 所有這些( UITableView )都顯示在UICollectionViewCell內部,該UICollectionViewCell占據整個屏幕,但允許用戶在各部分之間滑動。 表格中的所有其他按鈕都可以正常工作。

剛剛想出了解決方案。 因為按鈕是在UICollectionViewCell中啟動的,所以引用self無效。 當我在init方法中添加按鈕動作時,它會起作用:

MyTableView.tableFooterView?.addSubview(nextButton)
nextButton.addTarget(self, action: #selector(nextAction), for: .touchUpInside)

暫無
暫無

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

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