我正在尝试以编程方式关闭屏幕键盘,我的代码如下
override func viewDidLoad() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
}
func keyboardWillShow(notification: NSNotification) {
var info = notification.userInfo!
let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
UIView.animateWithDuration(0.1, animations: { () -> Void in
self.Bottomspace.constant = keyboardFrame.size.height
})
}
func keyboardWillHide(notification: NSNotification){
self.Bottomspace.constant = 0
}
它适用于 iPhone 6 和 iPad,但不适用于 iPhone 5 和 iPhone 5s,有什么建议吗?
最佳答案
试试这个:
// Dismiss Keyboard when user touch screen
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
让我知道它是否有效!
关于ios - 无法关闭 iPhone 5 和 iPhone 5s 的键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34914586/