swift4-页面随键盘抬起以及点击屏幕键盘落下

页面随着键盘抬起

extension viewController:UITextFieldDelegate {
    
    //输入框开始编辑
    func textFieldDidBeginEditing(_ textField: UITextField) {
        //view弹起跟随键盘,高可根据自己定义
        UIView.animate(withDuration: 0.4, animations: {
            
            self.view.frame.origin.y = -150
            
        })
    }
    
    //输入框结束编辑的时候
    func textFieldDidEndEditing(_ textField: UITextField) {
        //键盘收回,view放下
        
        UIView.animate(withDuration: 0.2, animations: {
            
            self.view.frame.origin.y = 0
            
        })
    }

    
}

 点击屏幕键盘下落

    // 点击屏幕收起键盘
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        view.endEditing(true)
        
    }

 

展开阅读全文

没有更多推荐了,返回首页