Created
July 26, 2017 14:22
-
-
Save e23z/4679ba2d7426e831d8fceb05afb96eff to your computer and use it in GitHub Desktop.
[Move Screen on Keyboard Show] How to move the screen to keep the input visible when the keyboard opens. #xamarin #ui #ux #ios
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
| // listen to keyboards events | |
| NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, OnKeyboardNotification); | |
| NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, OnKeyboardNotification); | |
| // reposition the view | |
| void OnKeyboardNotification(NSNotification notification) { | |
| var visible = notification.Name == UIKeyboard.WillShowNotification; | |
| var location = _origin.Location; | |
| if (visible) { | |
| var keyboardFrame = UIKeyboard.FrameEndFromNotification(notification); | |
| var field = this.FirstResponder(); | |
| if (field != null && field.Superview.Frame.GetMidY() > Frame.Height * 0.5f) | |
| location.Y -= keyboardFrame.Height * 0.5f; | |
| } | |
| else location = _origin.Location; | |
| Frame = new CGRect(location, Frame.Size); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment