Xamarin: Moving the view on Keyboard show

The keyboard on a phone normally covers around half the screen when it appears.  That means any control such as a UITextField in the lower half of the screen will be covered.  To prevent this we need to dynamically move the view up and down if a control is being hidden.

There are a variety of methods to handle this including placing items in a UITableView.  The method I will describe is a more of a manual method and is similar to the method recommended by apple here.

Here is my solution.

Variables for the controller that houses the textboxes

private UIView activeview; // Controller that activated the keyboard 
private float scroll_amount = 0.0f; // amount to scroll 
private float bottom = 0.0f; // bottom point 
private float offset = 10.0f; // extra offset 
private bool moveViewUp = false; // which direction are we moving

Keyboard observers for ViewDidLoad().

// Keyboard popup
NSNotificationCenter.DefaultCenter.AddObserver
(UIKeyboard.DidShowNotification,KeyBoardUpNotification);

// Keyboard Down
NSNotificationCenter.DefaultCenter.AddObserver
(UIKeyboard.WillHideNotification,KeyBoardDownNotification);

First up is the KeyboardUpNotification method.  Essentially you calculate if the control will be hidden by the keyboard and if so calculate how much the view needs to be moved to show the control, and then move it.

private void KeyBoardUpNotification(NSNotification notification)
{
    // get the keyboard size
    RectangleF r = UIKeyboard.BoundsFromNotification (notification);

    // Find what opened the keyboard
    foreach (UIView view in this.View.Subviews) {
        if (view.IsFirstResponder)
            activeview = view;
    }

    // Bottom of the controller = initial position + height + offset      
    bottom = (activeview.Frame.Y + activeview.Frame.Height + offset);

    // Calculate how far we need to scroll
    scroll_amount = (r.Height - (View.Frame.Size.Height - bottom)) ;

    // Perform the scrolling
    if (scroll_amount > 0) {
         moveViewUp = true;
         ScrollTheView (moveViewUp);
    } else {
         moveViewUp = false;
    }

}

The keyboard down event is simple.  If the View has been moved just reverse it.

private void KeyBoardDownNotification(NSNotification notification)
{
    if(moveViewUp){ScrollTheView(false);}
}

Scrolling the view will scroll the view in an animated manner.

private void ScrollTheView(bool move)
{

    // scroll the view up or down
    UIView.BeginAnimations (string.Empty, System.IntPtr.Zero);
    UIView.SetAnimationDuration (0.3);

    RectangleF frame = View.Frame;

    if (move) {
        frame.Y -= scrollamount;
    } else {
        frame.Y += scrollamount;
        scrollamount = 0;
    }

    View.Frame = frame;
    UIView.CommitAnimations();
}

Here is what happens without this code on a normal UIVIew.

Going to edit the 444.4 field.

Keyboard_PreScroll

But the keyboard covers it up

Keyboard_NoScroll

Now with the code added the view is scrolled.

Keyboard_Scrolled

Note in this example i have moved a UIView.  The same effect could be had by scrolling a UIScrollView.  The benefit of the method I described is all new items that bring up the keyboard will automatically be scrolled into view.