Search This Blog

Saturday 7 August 2021

Xamarin IOS Keyboard with done button.

Xamarin Form Project
BorderlessEntryWithDoneButton.cs
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;
 
namespace TestApp.Renderer
{
    public class BorderlessEntryWithDoneButton : Entry
    {
    }
}
Xamarin iOS Project
BorderlessEntryRendererWithDoneButton.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
 
using Foundation;
using TestApp.iOS.CustomRenderers;
using TestApp.Renderer;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
 
[assembly: ExportRenderer(typeof(BorderlessEntryWithDoneButton), typeof(BorderlessEntryRendererWithDoneButton))]
namespace TestApp.iOS.CustomRenderers
{
    public class BorderlessEntryRendererWithDoneButton : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
 
            if (Control != null && Element != null)
            {
                Control.BackgroundColor = Xamarin.Forms.Color.Transparent.ToUIColor();
                Control.Layer.BackgroundColor = Xamarin.Forms.Color.Transparent.ToCGColor();
                Control.BorderStyle = UITextBorderStyle.None;
                this.AddDoneButton();
            }
        }
 
        protected void AddDoneButton()
        {
            var toolbar = new UIToolbar(new RectangleF(0.0f, 0.0f, 50.0f, 44.0f));
            toolbar.BackgroundColor = UIColor.LightGray;
            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;
        }
    }
}


Implementations

 <renderer:BorderlessEntryWithDoneButton   VerticalOptions="Center"   Text="{Binding Title}"  Placeholder="Event Name" "  />


2 comments:

Popular Posts