我正在尝试将 UIButton 添加为 UICollectionViewCell 内另一个自定义 UIView 的 subview 但是,我添加的这两个按钮不会响应触摸,也不执行任何操作。
这里我正在初始化按钮:
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")
}
添加为 subview 并设置约束:
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 的 subview 时,按钮永远不会工作。
无论如何,我很乐意收到任何建议!
PS:我使用自定义扩展来设置约束。它们工作得很好,因为我经常在我的项目中使用它们。
谢谢!
最佳答案
你可以试试
newsImage.isUserInteractionEnabled = true
By default the
UserInteraction
property ofUIImageView
is set tofalse
. This the place where most of us makes mistake. So the main thing to check in while adding any control toUIImageView
is to set itsisUserInteractionEnabled
property totrue
.
关于ios - 在 UICollectionViewCell 中以编程方式添加的 UIButton 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45902490/