Skip to content

Instantly share code, notes, and snippets.

@bok-
Created November 15, 2018 05:49
Show Gist options
  • Save bok-/2e6c269458754e6af5c999974db7962d to your computer and use it in GitHub Desktop.
Save bok-/2e6c269458754e6af5c999974db7962d to your computer and use it in GitHub Desktop.
import UIKit
class ViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillResize(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
@objc func keyboardWillResize(_ notification: Notification) {
guard let frame = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }
let height = frame.intersection(self.view.safeAreaLayoutGuide.layoutFrame).height
UIView.animate(withDuration: 0.24, animations: {
self.additionalSafeAreaInsets.bottom = max(height, 0)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment