这里介绍一个非常神奇的坑
键盘弹出时点击背景按钮未响应,这边情况特殊在键盘弹出后,点击按钮颜色有变,但是点击事件却没有响应,一开始以为是IQKeyboardManagerSwift的坑,调试了半天跟这个没关系。后来各种方法尝试一下找到了解决方法。
var deleteButton: UIButton = {
let button = UIButton(type: .custom)
button.addTarget(self, action: #selector(deleteAction(_:)), for: .touchUpInside)
return button
}()
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setup()
}
// MARK: - Private
private func setup() {
self.addSubview(deleteButton)
deleteButton.snp.makeConstraints { make in
make.width.equalTo(50)
make.height.equalTo(40)
make.top.right.equalToSuperview()
}
}
// MARK: - Action
@objc func deleteAction(_ sender: UIButton) {
emptySearchHistoryClosure?()
}
解决方法:把存储属性改为延迟存储属性
总结:应该是我代码的编写方式比较奇特,不然应该不会出现这种神奇的bug
lazy var deleteButton: UIButton = {
let button = UIButton(type: .custom)
button.addTarget(self, action: #selector(deleteAction(_:)), for: .touchUpInside)
return button
}()
// MARK: - Action
@objc func deleteAction(_ sender: UIButton) {
emptySearchHistoryClosure?()
}