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<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }


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

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值