【发布时间】:2018-03-08 09:30:41
【问题描述】:
我有一个UICollectionViewCell 和里面的两个按钮,但是按钮不响应触摸,我以编程方式创建按钮并通过addSubView 将其添加到单元格中它可以工作,但是当我通过持有人添加时。addSubView 不是工作!
UserInteractionEnabeld的持有人是真的!
请帮忙
class FavoriteProductCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var holder: UIView!
override func awakeFromNib() {
super.awakeFromNib()
let btnFindBestPrice = UIButton(frame: CGRect(x: 0, y: 0, width: 151, height: 20))
btnFindBestPrice.setTitle("Find best price", for: .normal)
btnFindBestPrice.backgroundColor = UIColor.ButtonBackGreen
btnFindBestPrice.isUserInteractionEnabled = true
btnFindBestPrice.addTarget(self, action: #selector(findBestPrice), for: .touchUpInside)
// AddTarget not working
holder.addSubview(btnFindBestPrice)
// AddTarget works fine
addSubview(btnFindBestPrice)
}
}
【问题讨论】:
-
检查
holder.isUserInteractionEnabled是否设置为true。另外,检查holder是否足够大。您能否将其背景颜色设置为红色/绿色或其他任何值,并检查按钮是否对它来说不是太大?我最近收到了这个案子。按钮可见,但超级视图(在您的情况下为holder)太小,导致触摸被忽略。 -
分享 UI 你想如何实现这一点?
标签: uicollectionview uibutton uicollectionviewcell