I have implemented a sucessful piece of code to move a back button and next button at the bottom of my view to be above the keyboard when it shows and reset back to default when the keyboard disappears using the Notification Center.

I'm wondering how I'd do the same but when a datePicker pops up at the bottom?

This is the code that moves two of my Buttons at the bottom of the screen above the keyboard when it popups:

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)

@objc func keyboardWillShow(notification: NSNotification) {
    guard let userInfo = notification.userInfo else {return}
    guard let keyboardSize = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else {return}
    let keyboardFrame = keyboardSize.cgRectValue
    let animationDurarion = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! TimeInterval
    UIView.animate(withDuration: animationDurarion, animations: { () -> Void in
        self.backButton_constrant.constant += keyboardFrame.height
        self.nextButton_constrant.constant += keyboardFrame.height
        self.view.layoutIfNeeded()
    })
}

@objc func keyboardWillHide(notification: NSNotification){
    guard let userInfo = notification.userInfo else {return}
    guard let keyboardSize = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else {return}
    let keyboardFrame = keyboardSize.cgRectValue
    let animationDurarion = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! TimeInterval
    UIView.animate(withDuration: animationDurarion, animations: { () -> Void in
        self.backButton_constrant.constant -= keyboardFrame.height
        self.nextButton_constrant.constant -= keyboardFrame.height
        self.view.layoutIfNeeded()
    })

Now obviously that only works when a keyboard popups, not when a date picker popup, like this:

Picture of what the app looks like

(The drawn oval is where the buttons should be, however they're being covered up by the date picker now.)

Thanks

1 Answers

0
Community On Best Solutions

I fixed this since the date picker acts as a keyboard, so I could use the same code shown in my question above for the date picker too. The ‘NotificationCall’ is for any pop up for user input.