Swift: change place button according to the appearance of the keyboard

Asked

Viewed 359 times

0

I’m making an app in Xcode and I would like the button, which is at the bottom of the screen, to move according to keyboard appearance.

Thank you

Tela do Aplicativo

Tela do Aplicativo

1 answer

2

When the keyboard is displayed or inhibited, you can listen to notifications to process them. In your view controller, sign up to listen to these events as follows:

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: .UIKeyboardDidShow , object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidHide), name: .UIKeyboardDidHide , object: nil)
}

You should also create the functions you are reporting on selector:

@objc private func keyboardDidShow() {
    // Processe aqui para quando o teclado terminar de ser *exibido*
}

@objc private func keyboardDidHide() {
    // Processe aqui para quando o teclado terminar de ser *inibido*
}

There are four such events for the keyboard:

  • .UIKeyboardWillShow;
  • .UIKeyboardDidShow;
  • .UIKeyboardWillHide;
  • .UIKeyboardDidHide.

You can sign any of them to listen to keyboard related changes.

A longer list of events related to your application can be found here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.