Created
November 15, 2018 05:49
-
-
Save bok-/2e6c269458754e6af5c999974db7962d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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