繁体   English   中英

UICollectionViewHeader中的UIButton不起作用

[英]UIButton in UICollectionViewHeader doesn't work

编辑:该代码在单元格的正文中工作,但不在标题中!

我已经以编程方式向我的视图添加了一个按钮。 一切都正确显示。 当我按下按钮时,它会突出显示。 问题在于该函数没有被调用。 有人有解决问题的想法吗?

let addPerson : UIButton = {
    let btn = UIButton(type: .system)
    btn.setTitle("Test button", for: .normal)
    btn.setTitleColor(.white, for: .normal)
    btn.backgroundColor = .darkGray
    btn.layer.masksToBounds = true
    btn.addTarget(self, action: #selector(handleAddPerson), for: .touchUpInside)
    btn.translatesAutoresizingMaskIntoConstraints = false
    return btn
}()

@objc func handleAddPerson() {
    print("test")
}


//Following is inside the init
let stackView = UIStackView(arrangedSubviews: [addPerson,addGroup])
    stackView.distribution = .fillEqually
    stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.spacing = 10
    addSubview(stackView)

    NSLayoutConstraint.activate([
        stackView.topAnchor.constraint(equalTo: topAnchor, constant: 8),
        stackView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8),
        stackView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8),
        stackView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -8),
        ])

尝试将选择器更改为该配合:

@objc func handleAddPerson(_ sender: UIButton?) {
    print("test")
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM