UICollectionViewHeader中的UIButton不起作用

时间:2019-03-04 13:10:44

标签: ios swift uikit

编辑:该代码在单元格的主体中起作用,但不适用于标题!

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

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),
        ])

1 个答案:

答案 0 :(得分:0)

尝试将选择器更改为此伴侣:

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