以编程方式在UICollectionViewCell的IndexPath中的多个UIButton,下一个addTarget不会单击
我以编程方式在UICollectionViewCell
的indexPath中创建了多个UIButton。现在我想打印“点击添加到收藏夹”,但点击UIButton
后没有打印任何东西。 UIButton
确实查看除addTarget
函数外的所有内容,而不是单击。以编程方式在UICollectionViewCell的IndexPath中的多个UIButton,下一个addTarget不会单击
import UIKit
//**UICollectionViewCell**
class DescriptionCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
override init(frame: CGRect) {
super.init(frame: frame)
setupCell()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
lazy var addToCart: UIButton = {
let btn = UIButton()
btn.setTitle("Add to favourite", for: .normal)
btn.isUserInteractionEnabled = true
btn.setTitleColor(.white, for: .normal)
btn.backgroundColor = UIColor.orange
btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 8)
btn.titleLabel?.textAlignment = .right
// addToCart not click
btn.addTarget(self, action: #selector(addToCartTarget), for: .touchUpInside)
btn.layer.cornerRadius = 12
return btn
}()
func setupCell() {
addSubview(addToCart)
addConstraintsWithFormat("H:|-80-[v0]-80-|", views: addToCart)
addConstraintsWithFormat("V:|-380-[v0(25)]|", views: addToCart)
}
func addToCartTarget() {
print("you clicked add to favourite")
}
}
尝试用类似框架初始化按钮:
let btn = UIButton(frame: CGRect(x: 10, y: 10, width: 50, height: 20))
一切ok了UIButton的,只是addTarget不显示任何信息后点击按钮 –
我测试您的代码段和它的工作对我来说很好。 –
对不起!我忘了说“我加了多重的UIButton在一个collectionViewCell一个indexPath” –
@AzmalTech必须有与按钮视图不addTarget问题,尝试用唯一的目标首先添加简单的按钮,检查视图是否在屏幕上正确设计 – Abhishek
@Abhishek视图设计正确显示 –
您可以使用Xcode的视图调试来检查动态按钮框以验证其大小 – Abhishek