swift - UIButton 不会指向目标
问题描述
我有一个 UICollectionViewController 被用作我的主要 VC,然后一个 UICollectionViewCell 设置为顶部作为标题。但是当我尝试按下单元格内的按钮时,我无法得到回应。我正在为我的 viewDidLoad 使用 initWithFrame。此单元格上的所有按钮都是这种情况。有什么想法吗?
UICollectionViewCell 或标头中的代码
let editProfileFollowButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Loading", for: .normal)
button.layer.cornerRadius = 3
button.layer.borderColor = UIColor.lightGray.cgColor
button.layer.borderWidth = 0.5
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
button.setTitleColor(.black, for: .normal)
button.isEnabled = true
button.addTarget(self, action: #selector(handleButtonFunction), for: .touchUpInside)
return button
}()
@objc func handleButtonFunction() {
print("button clicked")
}
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(editProfileFollowButton)
editProfileFollowButton.anchor(top: postLabel.bottomAnchor, left: postLabel.leftAnchor, bottom: nil, right: self.rightAnchor, paddingTop: 12, paddingLeft: 12, paddingBottom: 0, paddingRight: 20, width: 0, height: 30)
}
解决方案
您可以尝试两种选择。首先,尝试cell.bringSubviewToFront(cell.button)
。如果这不起作用,请尝试cell.contentView.isUserInteractionEnabled = false
.
推荐阅读
- python - 安装 Python 3 模块时的问题
- typescript - 从 firebase-realtime-database 过滤数据
- python - 使用 Python 的 XML 解析问题
- javascript - Laravel MIX,如何在多个文件中使用单个函数
- angular - 构建利用 InjectionToken 的库模块时“无法读取 null 的属性‘类型’”
- tfs - 什么决定了 NuGet 包的 .NET 版本子文件夹和 HintPath,我们如何更改它们?
- javascript - JavaScript:如何重命名 JSON 文件的标题名称
- java - Jackson ObjectMapper readValue 类属性更改时的预期行为
- python - 从 SQL Server 数据库获取表列表作为干净的 Python 字符串列表
- python - Celery task_success 信号参数