【发布时间】:2017-07-15 03:09:50
【问题描述】:
我正在向集合视图单元格的自定义类添加一个按钮,但无法点击它。
这是我在单元格自定义类中声明按钮的方式:
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")
}
我如何在初始化中添加按钮
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 和 self 但都给出相同的结果,这是不可点击的按钮。
欢迎提出任何建议。
【问题讨论】:
标签: ios swift uibutton uicollectionviewcell