0

I'm trying to move up a UIButton when the keyboard is shown. So when the keyboard is shown, the UIButton moves up 20px above the keyboard at the same time the keyboard animation is shown so it look like the button is connected to the keyboard. So I need to determine the size of the keyboard so I can apply that to the button event. Here is my code:

 @objc func keyboardWillChange(notification: Notification) {
        if notification.name == UIResponder.keyboardWillShowNotification || notification.name == UIResponder.keyboardWillChangeFrameNotification {

            nextBtn.frame.origin.y = view.frame.maxY - 300
        } else {
            nextBtn.frame.origin.y = 0
        }
    }

I have a few problems with my code:

class Start_EmailVC: UIViewController, UITextFieldDelegate {

    var checkValid = CheckValid()

    @IBOutlet weak var nextBtn: UIButton!

    @IBOutlet weak var emailTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        //Next Button Auth
        nextBtn.alpha = 0.5
        nextBtn.isEnabled = false

        hideIt()

        NotificationCenter.default.addObserver(self, selector: #selector(keyboardShown), name: UIResponder.keyboardWillShowNotification, object: nil);
    }

    @objc func keyboardShown(notification: NSNotification) {
        let info = notification.userInfo!
        let keyboardFrame: CGRect = (info[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        print("keyboardFrame: \(keyboardFrame)")
    }

    func hideIt() {
        //Listen For Keyboard Events
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
    }

    deinit {
        //Stop Listening for keyboard hide/show events
        NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
        NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
    }

    @objc func keyboardWillChange(notification: Notification) {
        if notification.name == UIResponder.keyboardWillShowNotification || notification.name == UIResponder.keyboardWillChangeFrameNotification {

            nextBtn.frame.origin.y = view.frame.maxY - 300
        } else {
        nextBtn.frame.origin.y = 0
    }
}}

The button does push up but not the same time as the keyboard and it just doesn't look good.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Etiekyed
  • 55
  • 6
  • You need to animate the change. You can get the duration used through the `UIResponder.keyboardAnimationDurationUserInfoKey` key and the animation curve used using `UIResponder.keyboardAnimationCurveUserInfoKey` – MadProgrammer Nov 26 '18 at 02:23
  • Possible duplicate [Move Button & View when Keyboard Appears in Swift](https://stackoverflow.com/questions/53000282/move-button-view-when-keyboard-appears-in-swift/53019152#53019152) – MadProgrammer Nov 26 '18 at 02:26
  • 1
    Possible duplicate of [How to get height of Keyboard?](https://stackoverflow.com/questions/31774006/how-to-get-height-of-keyboard) – Nilanshu Jaiswal Nov 26 '18 at 02:32
  • Use this demo -> https://medium.com/@thedan84/how-to-set-up-a-keyboard-input-accessory-view-programmatically-4f78f0420351 – dahiya_boy Nov 26 '18 at 04:56
  • https://github.com/RxSwiftCommunity/RxKeyboard – SPatel Nov 26 '18 at 05:00

0 Answers0