DEV Community

Cover image for DONE button to keyboard in .NET MAUI
Victor Hugo Garcia
Victor Hugo Garcia

Posted on • Updated on

DONE button to keyboard in .NET MAUI

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
        });
    }
}
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
marklindsay99 profile image
marklindsay99

this was very helpful!

Collapse
 
smkms profile image
Sameer

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
});