如何使UITextView键盘不被tapping_Swift消除

时间:2018-01-09 07:19:41

标签: ios swift textview uitextview

我对键盘有疑问 如果我想在点击时继续显示键盘,我应该怎么做 我还标记了addObserver函数“ keyboardWillHide ”,但键盘也被忽略了。
感谢。

这是代码:

override func viewDidLoad() {
    super.viewDidLoad()

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name:.UIKeyboardWillShow, object: nil)
//NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name:.UIKeyboardWillHide, object: nil)

}

override func keyboardWillShow(notification: NSNotification) {
    super.keyboardWillShow(notification: notification)

    if let userInfo:NSDictionary = notification.userInfo as NSDictionary? {
        if let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as? NSValue {
            keyboardHeight = keyboardFrame.cgRectValue.height
        }
    }
    setUserMode(mode: .keyboard)
    updateViewFrame()
}

override func keyboardWillHide(notification: NSNotification) {

    keyboardHeight = 0
    updateViewFrame()
}

func updateViewFrame() {
    var diffHeight:CGFloat = UIApplication.shared.statusBarFrame.height
    if let nav = navigationController {
        diffHeight += nav.navigationBar.height
    }
    let newFrame = CGRect(x: 0, y: diffHeight, width: ScreenWidth, height: ScreenHeight-keyboardHeight-diffHeight)

    if view.frame.equalTo(newFrame) == false  {
        view.frame = newFrame
        self.tableView.frame = newFrame
    }
}

图片在这里:

image

1 个答案:

答案 0 :(得分:0)

请致电:

YourTextView.keyboardDismissMode = .none