Skip to content

Instantly share code, notes, and snippets.

@e23z
Created July 26, 2017 14:22
Show Gist options
  • Select an option

  • Save e23z/4679ba2d7426e831d8fceb05afb96eff to your computer and use it in GitHub Desktop.

Select an option

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
// 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