Xamarin.Forms example of how to move Xamarin.Forms elements with the iOS soft keyboard so they're not covered up
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
XamarinExamples.Forms.MoveWithKeyboard
iOS
.gitignore
LICENSE
README.md
XamarinExamples.Forms.MoveWithKeyboard.sln

README.md

XamarinExamples.Forms.MoveWithKeyboard

This repository holds an example of the best way I've been able to come up with to handle moving Xamarin.Forms elements as to not be covered by the iOS soft keyboard when it appears.

I hope this is able to lend a hand to anyone else looking for a simple solution to this problem.

Cheers!

How it works

Xamarin.Forms Code

In the Xamarin.Forms project we setup a very basic Custom Renderer (in this case, but a Button element) that tracks a boolean value for "MoveWithKeyboard".

public class CustomButton : Button
    {
        public const string MoveWithKeyboardName = "MoveWithKeyboard";

        public CustomButton() { }

        public static readonly BindableProperty MoveWithKeyboardProperty = BindableProperty.Create(
            propertyName: MoveWithKeyboardName,
            returnType: typeof(bool),
            declaringType: typeof(CustomButton),
            defaultValue: false);

        public bool MoveWithKeyboard
        {
            get { return (bool)GetValue(MoveWithKeyboardProperty); }
            set { SetValue(MoveWithKeyboardProperty, value); }
        }
    }

Then in our XAML we use the custom renderer for the button:

<custom:CustomButton MoveWithKeyboard="true" .... />

Xamarin.iOS

For the Xamarin.iOS bit, we create a Custom Renderer for the Button element.

(Link to where the magic happens in the IOS Custom Renderer)

protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
        ... setup notifications ...
}

In the OnElementChanged method, we setup two observers and register them with NSNotificationCenter that we register the events on both the UIKeyboard.WillShowNotification and UIKeyboard.WillHideNotification which will invoke our delegates when the keyboard is displayed and hidden.

FYI

Worth noting, there's some hackery around ensuring the Touch event is fired. It appears in Xamarin.Forms "Touch" really means "TouchUp". The issue we run into while relocating a button is that "TouchDown" triggers the keyboard to be hidden and thus triggering the notification event to relocate the button back to its original position.

Because the button is moved, the "TouchUp" event is not fired. The custom renderer in the iOS project has my workaround for this issue, which I have opened a Bugzilla case for (#58263).

Cheers!

Example of moving button