我正在向集合視圖單元格的自定義類中添加一個按鈕,但無法使其點擊。單擊CollectionViewCell中的按鈕
下面是我在單元格的自定義類聲明按鈕:
let shareBtn: UIButton = {
let roundBtn = UIButton()
roundBtn.frame = CGRect(x: 0, y: 0, width: 70, height: 70)
roundBtn.layer.cornerRadius = 35
roundBtn.layer.shadowOpacity = 0.25
roundBtn.layer.shadowRadius = 2
roundBtn.setImage(UIImage(named: "share"), for: .normal)
roundBtn.addTarget(self, action: #selector(shareAction(button:)), for: .touchUpInside)
roundBtn.isUserInteractionEnabled = true
roundBtn.isEnabled = true
return roundBtn
}()
這裏是方法的選擇調用:
func shareAction(button: UIButton){
print("shareAction")
}
在這裏,我如何在init將按鈕添加
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(shareBtn)
shareBtn.translatesAutoresizingMaskIntoConstraints = false
shareBtn.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -100).isActive = true
shareBtn.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
shareBtn.widthAnchor.constraint(equalToConstant: 70).isActive = true
shareBtn.heightAnchor.constraint(equalToConstant: 70).isActive = true
我試着將按鈕添加到兩個 - contentView和自我,但都給出了相同的結果,wh ich是不可點擊的按鈕。
歡迎任何建議。
是什麼?你是對的。爲什麼會迅速做到這一點,爲什麼要創建它的新實例,我不明白。謝謝 – fullMoon