Xamarin.Forms iOS Numeric Keyboard With 'Done' Button
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Xamarin.Forms; | |
namespace YourNamespace.Controls | |
{ | |
public class ExtendedEntry : Entry { } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Based on example from: https://forums.xamarin.com/discussion/18346/add-done-button-to-keyboard-on-ios | |
*/ | |
using System.Drawing; | |
using YourNamespace.Controls; | |
using YourNamespace.iOS.Renderers; | |
using UIKit; | |
using Xamarin.Forms; | |
using Xamarin.Forms.Platform.iOS; | |
[assembly: ExportRenderer(typeof(ExtendedEntry), typeof(ExtendedEntryRenderer))] | |
namespace YourNamespace.iOS.Renderers | |
{ | |
public class ExtendedEntryRenderer : EntryRenderer | |
{ | |
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) | |
{ | |
base.OnElementChanged(e); | |
if (Element == null) | |
return; | |
// Check only for Numeric keyboard | |
if (this.Element.Keyboard == Keyboard.Numeric) | |
this.AddDoneButton(); | |
} | |
/// <summary> | |
/// <para>Add toolbar with Done button</para> | |
/// </summary> | |
protected void AddDoneButton() | |
{ | |
var toolbar = new UIToolbar(new RectangleF(0.0f, 0.0f, 50.0f, 44.0f)); | |
var doneButton = new UIBarButtonItem(UIBarButtonSystemItem.Done, delegate | |
{ | |
this.Control.ResignFirstResponder(); | |
var baseEntry = this.Element.GetType(); | |
((IEntryController)Element).SendCompleted(); | |
}); | |
toolbar.Items = new UIBarButtonItem[] { | |
new UIBarButtonItem (UIBarButtonSystemItem.FlexibleSpace), | |
doneButton | |
}; | |
this.Control.InputAccessoryView = toolbar; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment