Move up to place the keyboard

I have a view with several text fields, and I want to make the same effect as the Contacts application when you click on a text field, otherwise it will be hidden by the keyboard when it appears. When I fire the keyboard, I plan to move the view correctly.

I suspect that I am doing this by changing the value of Frame, but I need it to be animated so that it does not annoy the user.

Tips? Examples?

+4
source share
3 answers

Wrapping your view in a UIScrollView is really a way. Besides the delegate textFieldDidEndEditing you could sign up for UIKeyboardDidHideNotification and UIKeyboardDidShowNotification , and when you get a notification that the keyboard is hiding / showing, then scroll your view accordingly. I can send code examples for keyboard notifications if you need it :)

Edit It is clear that I would publish the code anyway - it might seem useful to someone:

You need to declare listeners for notifications:

 NSObject hideObj = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.DidHideNotification, HandleKeyboardDidHide); NSObject showObj = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.DidShowNotification, HandleKeyboardDidShow); 

then your actions will look something like this:

 void HandleKeyboardDidShow(NSNotification notification) { scrollView.ScrollRectToVisible(textfield.Frame, true); } void HandleKeyboardDidHide(NSNotification notification) { // scroll back to normal } 

Edit 2

So, if you want to remove Observers when you destroy the view, you first need to make sure that you assign NSObject when adding observers, and then use the following code to remove them:

 NSNotificationCenter.DefaultCenter.RemoveObserver(showObj); NSNotificationCenter.DefaultCenter.RemoveObserver(hideObj); 

Hope this helps.

+6
source

I just did it in the app. I used scrollview to wrap my entire view, and then used the scrollToRectVisible method in the textFieldDidEndEditing-delegate method. It works great!

+2
source

Apple's keyboard management documentation is pretty good and contains code (below) for most situations that you can copy / paste directly into your application.

Good luck.

+1
source

Source: https://habr.com/ru/post/1342119/


All Articles