LazyBlog

首先在您的viewDidLoad呼叫此方法,如下

1
2
3
4
override func viewDidLoad() {
super.viewDidLoad()
self.hideKeyboardWhenTappedAround()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
//默認是true,若不關閉會無法觸發didSelect,看自身需求

tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}

func dismissKeyboard() {
view.endEditing(true)
}
}

 評論