增加手勢讓鍵盤隱藏 League2eb 2019-09-16 iOS 首先在您的viewDidLoad呼叫此方法,如下 1234override func viewDidLoad() { super.viewDidLoad() self.hideKeyboardWhenTappedAround()} 12345678910111213extension 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) }} 更新於 2020年6月6日 UIViewController Swift 上一頁 螢幕橫向時的畫面配置 UIViewController Swift 下一頁 Swift跳轉設定 Swift UIApplication