Swfit 3.0 软键盘挡住视图的解决办法

设置一个全局变量:

var keyBoardNeedLayout: Bool = true

viewDidLoad()中注册通知中心:

override func viewDidLoad() {
        super.viewDidLoad()
        
        // 注册通知中心,监听键盘弹起的状态
       NotificationCenter.default.addObserver(self,selector: #selector(self.keyboardWillShow(_:)),name: .UIKeyboardWillShow, object: nil)

        // 注册通知中心,监听键盘回落的状态
        NotificationCenter.default.addObserver(self,selector: #selector(self.keyboardWillHide(_:)),name: .UIKeyboardWillHide, object: nil)
}

键盘弹出的响应方法:

    func keyboardWillShow(_ notification: NSNotification) {
        print("show")
        if let userInfo = notification.userInfo,
            let value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue,
            let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double,
            let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt {
            
            let frame = value.cgRectValue
            let intersection = frame.intersection(self.view.frame)
            
            let deltaY = intersection.height
            
            if keyBoardNeedLayout {
                UIView.animate(withDuration: duration, delay: 0.0,
                                           options: UIViewAnimationOptions(rawValue: curve),
                                           animations: { _ in
                                            self.view.frame = CGRect(x:0, y: -deltaY, width: self.view.bounds.width, height: self.view.bounds.height)
                                            self.keyBoardNeedLayout = false
                                            self.view.layoutIfNeeded()
                    }, completion: nil)
            }
            
            
        }
    }

键盘隐藏的响应方法:

    func keyboardWillHide(_ notification: NSNotification) {
        print("hide")
        if let userInfo = notification.userInfo,
            let value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue,
            let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double,
            let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt {
            
            let frame = value.cgRectValue
            let intersection = frame.intersection(self.view.frame)
            
            let deltaY = intersection.height
            
            UIView.animate(withDuration: duration, delay: 0.0,
                                       options: UIViewAnimationOptions(rawValue: curve),
                                       animations: { _ in
                                        self.view.frame = CGRect(x:0, y: deltaY, width: self.view.bounds.width, height: self.view.bounds.height)
                                        self.keyBoardNeedLayout = true
                                        self.view.layoutIfNeeded()
                }, completion: nil)
            
        }
    }


点击屏幕任意空白处触发键盘回落:

    override func touchesBegan(_ touches: Set, with event: UIEvent?) {
        view.endEditing(true)
    }


注意:如果想要实现单击软件盘的Return(返回)键同样触发键盘回落,请继承 UITextFieldDelegate,并设置textField.delegate = self,再添加如下代码:

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        
        textField.resignFirstResponder()
        
        return true
    }


你可能感兴趣的:(ios)