In this article, I'm going to show you how to add a DONE button to the keyboard in .NET MAUI by using a custom handler for iOS compatible with iOS 15+.
Create a static class EntryHandler
using Microsoft.Maui;
using System.Drawing;
#if IOS
using UIKit;
using Foundation;
#endif
namespace DemoMauiApp.Handlers;
public class EntryHandler
{
public static void AddDone()
{
Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("Done", (handler, view) =>
{
#if IOS
var toolbar = new UIToolbar(new RectangleF(0.0f, 0.0f, 50.0f, 44.0f));
toolbar.BackgroundColor = UIColor.LightGray; // Set the color you prefer
var doneButton = new UIBarButtonItem(UIBarButtonSystemItem.Done, delegate
{
handler.PlatformView.ResignFirstResponder();
});
toolbar.Items = new UIBarButtonItem[] {
new UIBarButtonItem (UIBarButtonSystemItem.FlexibleSpace),
doneButton
};
handler.PlatformView.InputAccessoryView = toolbar;
#endif
});
}
}
Register the handler
On the MauiProgram.cs file register the handler
EntryHandler.AddDone();
Conclusion
This is a port from our friend yuv4ik at github (thanks for sharing it) with a few modifications to add the DONE button to all types of keyboards in .NET MAUI using handlers.
Thanks for reading! Follow me on Twitter @ivictorhugo
Top comments (2)
this was very helpful!
Completed event in the entry can be triggered with following slight modification incase logic is dependent on Completed event:
var doneButton = new UIBarButtonItem(UIBarButtonSystemItem.Done, delegate
{
handler.PlatformView.ResignFirstResponder();
handler.VirtualView?.Completed(); // <--- Triggers completed event
});