在UICollectionViewCell中以编程方式添加UIButton不起作用
问题描述:
我想将UIButton作为UICollectionViewCell中另一个自定义UIView的子视图添加 但是,我添加的这两个按钮不响应触摸并且不执行任何操作。在UICollectionViewCell中以编程方式添加UIButton不起作用
我在这里初始化按钮:
let likeButton: UIButton = {
let button = UIButton(type: .system)
button.anchor(width: 40, height: 40)
button.backgroundColor = UIColor(red: 239.0/255.0, green: 83.0/255.0, blue: 80.0/255.0, alpha: 0.8)
button.layer.cornerRadius = 20
button.layer.masksToBounds = true
button.isUserInteractionEnabled = true
return button
}()
let commentButton: UIButton = {
let button = UIButton(type: .system)
button.backgroundColor = UIColor(red: 38.0/255.0, green: 37.0/255.0, blue: 37.0/255.0, alpha: 0.8)
button.layer.cornerRadius = 20
button.layer.masksToBounds = true
return button
}()
override init(frame: CGRect) {
super.init(frame: frame)
likeButton.addTarget(self, action: #selector(handleLike), for: .touchUpInside)
commentButton.addTarget(self, action: #selector(handleComment), for: .touchUpInside)
setupViews()
}
func handleLike(_ sender: UIButton) {
print("like")
}
func handleComment(_ sender: UIButton) {
print("comment")
}
添加为子视图并设置限制条件:
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
func setupViews() {
let stackView = UIStackView(arrangedSubviews: [likesLabel, commentsLabel, viewsLabel])
stackView.axis = .horizontal
stackView.distribution = .fillProportionally
stackView.spacing = 4
addSubview(newsImage)
newsImage.addSubview(darkView)
darkView.addSubview(newsTitleLabel)
darkView.addSubview(stackView)
darkView.addSubview(timeStampLabel)
setButtonsStackView()
addConstraintsWithFormaat(format: "H:|[v0]|", views: newsImage)
addConstraintsWithFormaat(format: "V:|[v0]|", views: newsImage)
newsImage.addConstraintsWithFormaat(format: "H:|[v0]|", views: darkView)
newsImage.addConstraintsWithFormaat(format: "V:|[v0]|", views: darkView)
stackView.anchor(top: nil, left: leftAnchor, bottom: bottomAnchor, right: nil, paddingTop: 0, paddingLeft: 10, paddingBottom: -10, paddingRight: 0)
newsTitleLabel.anchor(top: nil, left: leftAnchor, bottom: stackView.topAnchor, right: nil, paddingTop: 0, paddingLeft: 10, paddingBottom: -5, paddingRight: 0)
timeStampLabel.anchor(top: nil, left: nil, bottom: bottomAnchor, right: rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: -10, paddingRight: 10)
}
func setButtonsStackView() {
let buttonStackView = UIStackView(arrangedSubviews: [likeButton, commentButton])
buttonStackView.axis = .horizontal
buttonStackView.distribution = .fillProportionally
buttonStackView.spacing = 10
darkView.addSubview(buttonStackView)
buttonStackView.anchor(top: darkView.topAnchor, left: nil, bottom: nil, right: darkView.rightAnchor, paddingTop: 10, paddingLeft: 10, paddingBottom: 0, paddingRight: 10)
}
正如我正在考虑这个问题可能与设置的限制,也因为我从读另一篇文章中,当你添加一个按钮作为UIImageView的子视图时,这些按钮从不起作用。
无论如何,我会很乐意得到任何建议!
PS:我使用自定义扩展来设置约束。他们工作得很好,因为我经常在我的项目中使用它们。
谢谢!
答
我刚刚意识到我必须设置buttonStackView与cellView的contentView相关的约束。
addSubview(buttonStackView)
buttonStackView.anchor(top: topAnchor, left: nil,
bottom: nil, right: rightAnchor,
paddingTop: 10, paddingLeft: 10,
paddingBottom: 0, paddingRight: 10)
之后,所有的按钮工作正常,并响应触摸。
将按钮设置为UIImageView的子视图实际上是主要问题。
答
你可以试试,
默认的
UIImageView
的UserInteraction
属性设置为false
。这是我们大多数人犯错的地方。所以主 东西要加入任何控制UIImageView
是检查 其isUserInteractionEnabled
财产true
。