UIKeyboardBoundsUserInfoKey is deprecated, what to use instead?

前端 未结 9 1779
我在风中等你
我在风中等你 2020-12-02 04:56

I\'m working on an iPad app using 3.2 sdk. I\'m dealing with obtaining the keyboard size to prevent my textfields from hidding behind it.

I\'m getting a Warning in

9条回答
  •  青春惊慌失措
    2020-12-02 05:34

    Its worked like this

    This is the constraint of the save button bottom

    @IBOutlet weak var saveBtnBottom: NSLayoutConstraint!
    @IBOutlet weak var nameText: UITextField!
    

    Inside viewDidLoad

    NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
    nameText.delegate = self
    

    This is the functions we need

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        nameText.resignFirstResponder()
        return true
    }
    
    
    @objc func keyBoardWillShow(notification: Notification){
        if let userInfo = notification.userInfo as? Dictionary{
            let frame = userInfo[UIResponder.keyboardFrameEndUserInfoKey]
            let keyBoardRect = frame?.cgRectValue
            if let keyBoardHeight = keyBoardRect?.height {
                self.saveBtnBottom.constant = keyBoardHeight 
    
                UIView.animate(withDuration: 0.5, animations: {
                    self.view.layoutIfNeeded()
                })
            }
        }
    }
    
    @objc func keyBoardWillHide(notification: Notification){
        self.saveBtnBottom.constant = 30.0
        UIView.animate(withDuration: 0.5, animations: {
            self.view.layoutIfNeeded()
        })
    }
    

提交回复
热议问题