我有一个UIView(来自UIViewController)接收触摸和删除键盘。在同一个屏幕中,我有一个显示密码的UIButton,当我触摸UIButton时,触摸会转到UIView并移除键盘。
如何阻止UIView接收来自UIButton的触摸?
我试试0: [A, beer, 5], [A, beer, 439]
1: [A, wine, 123], [A, wine, 1293]
2: [B, beer, 493], [B beer, 91]
3: [B, wine, 944]
但是没有用。
删除键盘的扩展程序:
.isExclusiveTouch
在我的ViewController中,按钮:
import UIKit
public extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(
target: self,
action: #selector(UIViewController.dismissKeyboard)
)
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
func dismissKeyboard() {
view.endEditing(true)
}
}
答案 0 :(得分:1)
请勿使用手势只使用此代码
override func touchesBegan(_ touches: Set<UITouch>, with event:UIEvent?) {
if let touch = touches.first {
view.endEditing(true)
}
super.touchesBegan(touches, with: event)
}
当你触摸视图的任何部分(不是按钮)时,键盘将被代码解除(view.endEditing(true))
答案 1 :(得分:0)
尝试将userIntrecationEnabled设置为false。
//example
yourView.isUserInteractionEnabled = false
如果为视图禁用了用户交互,则会忽略触摸。