DEV Community

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

Posted on • Edited on

11

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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

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

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay