For iOS
There's two ways of doing this!
First Way: Implement it for one UIViewController, if your app is small, as follows:
Step 1: Add fields for the show and hide observers:
private NSObject _keyboardObserverWillShow;
private NSObject _keyboardObserverWillHide;
Step 2: Update the ViewDidLoad and ViewDidUnload overrides to add/remove the observers to whenever the keyboard is displayed or hidden:
public override void ViewDidLoad() {
base.ViewDidLoad ();
_keyboardObserverWillShow = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.DidShowNotification, KeyboardDidShowNotification);
_keyboardObserverWillHide = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, KeyboardWillHideNotification);
}
public override void ViewDidUnload() {
base.ViewDidUnload();
NSNotificationCenter.DefaultCenter.RemoveObserver(_keyboardObserverWillShow);
NSNotificationCenter.DefaultCenter.RemoveObserver(_keyboardObserverWillHide);
}
Step 3: Then you populate the functions KeyboardDidShowNotification
& KeyboardWillHideNotification
that execute for each observer. It's fairly long and would take me a while to explain each part of, but you can ask me in the comments. It goes as follows:
private void KeyboardWillHideNotification (NSNotification notification)
{
UIView activeView = View.FindFirstResponder();
if (activeView == null)
return;
UIScrollView scrollView = activeView.FindSuperviewOfType (this.View, typeof(UIScrollView)) as UIScrollView;
if (scrollView == null)
return;
// Reset the content inset of the scrollView and animate using the current keyboard animation duration
double animationDuration = UIKeyboard.AnimationDurationFromNotification(notification);
UIEdgeInsets contentInsets = new UIEdgeInsets(0.0f, 0.0f, 0.0f, 0.0f);
UIView.Animate(animationDuration, delegate{
scrollView.ContentInset = contentInsets;
scrollView.ScrollIndicatorInsets = contentInsets;
});
}
private void KeyboardDidShowNotification (NSNotification notification)
{
UIView activeView = View.FindFirstResponder();
if (activeView == null)
return;
((UITextField)activeView).ShowDoneButtonOnKeyboard();
UIScrollView scrollView = activeView.FindSuperviewOfType(this.View, typeof(UIScrollView)) as UIScrollView;
if (scrollView == null)
return;
RectangleF keyboardBounds = UIKeyboard.BoundsFromNotification(notification);
UIEdgeInsets contentInsets = new UIEdgeInsets(0.0f, 0.0f, keyboardBounds.Size.Height, 0.0f);
scrollView.ContentInset = contentInsets;
scrollView.ScrollIndicatorInsets = contentInsets;
// If activeField is hidden by keyboard, scroll it so it's visible
RectangleF viewRectAboveKeyboard = new RectangleF(this.View.Frame.Location, new SizeF(this.View.Frame.Width, this.View.Frame.Size.Height - keyboardBounds.Size.Height));
RectangleF activeFieldAbsoluteFrame = activeView.Superview.ConvertRectToView(activeView.Frame, this.View);
// activeFieldAbsoluteFrame is relative to this.View so does not include any scrollView.ContentOffset
// Check if the activeField will be partially or entirely covered by the keyboard
if (!viewRectAboveKeyboard.Contains(activeFieldAbsoluteFrame)) {
// Scroll to the activeField Y position + activeField.Height + current scrollView.ContentOffset.Y - the keyboard Height
PointF scrollPoint = new PointF(0.0f, activeFieldAbsoluteFrame.Location.Y + activeFieldAbsoluteFrame.Height + scrollView.ContentOffset.Y - viewRectAboveKeyboard.Height);
scrollView.SetContentOffset(scrollPoint, true);
}
}
Second Way: Create a keyboard handler that you can use for each page through your BaseViewController
For Android:
You can follow the solutions mentioned in the stackOverflow Xamarin solution here((Activity)Forms.Context).Window.SetSoftInputMode(SoftInput.AdjustPan);
. Some times that doesn’t work, so you can use this to fix it. PS: Didn’t copy these answers because it doesn’t make sense to rewrite an answer.