【问题标题】:UIButton Inside Header Not Working标题内的 UIButton 不起作用
【发布时间】:2017-09-22 05:16:43
【问题描述】:

我一直在寻找这个答案,但似乎找不到。 我在集合视图上方有一个 sectionHeader。我在标题中添加了一个按钮,但该功能没有运行。介意快速浏览 1 分钟并帮助我吗?

import UIKit
class SearchHeader: UICollectionViewCell {

    let filterLbl: UILabel = {
        let label = UILabel()
        label.text = "Filter"
        label.font = UIFont.systemFont(ofSize: 18)
        return label
    }()

    let addFilterBtn: UIButton = {
        let button = UIButton(type: .system)
        button.setImage(#imageLiteral(resourceName: "addFilter").withRenderingMode(.alwaysOriginal), for: .normal)
        button.addTarget(self, action: #selector(handleAddFilter), for: .touchUpInside)
        return button

    }()

    func handleAddFilter(button: UIButton) {
        print(1234)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)

        backgroundColor = .white

        addSubview(filterLbl)
        addSubview(addFilterBtn)

        filterLbl.anchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: nil, paddingTop: 10, paddingLeft: 10, paddingBottom: 10, paddingRight: 0, width: 0, height: 0)
        addFilterBtn.anchor(top: topAnchor, left: filterLbl.rightAnchor, bottom: bottomAnchor, right: nil, paddingTop: 10, paddingLeft: 5, paddingBottom: 10, paddingRight: 0, width: 0, height: 0)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

【问题讨论】:

    标签: xcode8 swift3.2


    【解决方案1】:

    我找到了答案。

    lazy var addFilterBtn: UIButton = {
        let button = UIButton(type: .system)
        button.setImage(#imageLiteral(resourceName: "addFilter").withRenderingMode(.alwaysOriginal), for: .normal)
        button.addTarget(self, action: #selector(handleAddFilter), for: .touchUpInside)
        return button
    
    }()
    

    由于某种原因,您必须将 let 更改为惰性 var

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-07
      • 2011-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多