I know this question has asked several times, but I'm looking for an implementation that uses Swift 3.0. To be clear I have a Text VIEW, not a text FIELD.
I tried doing something like this...
In viewDidLoad()
:
NotificationCenter.default.addObserver(self, selector: Selector(("keyboardWillShow")), name:NSNotification.Name.UIKeyboardWillShow, object: nil);
NotificationCenter.default.addObserver(self, selector: Selector(("keyboardWillHide")), name:NSNotification.Name.UIKeyboardWillHide, object: nil);
Then I created these methods in the ViewController:
func keyboardWillShow(sender: NSNotification) {
self.view.frame.origin.y = -150
}
func keyboardWillHide(sender: NSNotification) {
self.view.frame.origin.y = 0
}
I get a crash when I select the text view.
You need to modify your add observer code as mentioned below. As per Swift 3 migration guide this is the new way that should be followed to declare the add observer of notification.
Code :
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
I have checked this with demo code with text view and notification center.