https://github.com/enusbaum/xamarinexamples.forms.movewithkeyboard
Xamarin.Forms example of how to move Xamarin.Forms elements with the iOS soft keyboard so they're not covered up
https://github.com/enusbaum/xamarinexamples.forms.movewithkeyboard
xamarin xamarin-forms xamarin-ios xamarin-renderers
Last synced: 3 months ago
JSON representation
Xamarin.Forms example of how to move Xamarin.Forms elements with the iOS soft keyboard so they're not covered up
- Host: GitHub
- URL: https://github.com/enusbaum/xamarinexamples.forms.movewithkeyboard
- Owner: enusbaum
- License: mit
- Created: 2018-02-19T23:15:49.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-02-19T23:24:27.000Z (over 7 years ago)
- Last Synced: 2025-02-07T08:48:41.592Z (5 months ago)
- Topics: xamarin, xamarin-forms, xamarin-ios, xamarin-renderers
- Language: C#
- Homepage:
- Size: 16.6 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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".
```csharp
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:
```xml
```
### Xamarin.iOS
For the Xamarin.iOS bit, we create a Custom Renderer for the Button element.
([**Link**](https://github.com/enusbaum/XamarinExamples.Forms.MoveWithKeyboard/blob/c07521ed7450b622405639c4de971248da31e21e/iOS/Renderers/CustomButton_IOS.cs#L84-L132) to where the magic happens in the IOS Custom Renderer)
```csharp
protected override void OnElementChanged(ElementChangedEventArgs 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](https://bugzilla.xamarin.com/show_bug.cgi?id=58263)).
Cheers!
