How to Add Done Button to iOS Keyboard InputAccessoryView

I would like to add a "Done" button to the top of the SfAutoComplete's keyboard when it is open. I found this code online that allows me to add a custom done button to the regular entry via a Custom Renderer. The code to add the button works however when I attempt to close the keyboard by tapping the my app crashes and gives me a System.InvalidCastException when I try converting the Element to an IEntryController. I tried simply calling Element.Unfocus() to close the keyboard instead of the line that causes the crash but the keyboard doesn't close. How can I successfully get my custom done button to close the SfAutoComplete Keyboard on iOS? 


Thanks,
Bryson


Here is my code:

public class MySfAutoCompleteRendererRed : SfAutoCompleteRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<SfAutoComplete> e)
    {
        base.OnElementChanged(e);
        if (Element == null) return;
        if (Element.Keyboard == Keyboard.Numeric || Element.Keyboard == Keyboard.Telephone) AddDoneButton();
        SetReturnType(((MySfAutoCompleteRed)Element).ReturnType);
        Control.TintColor = ((Xamarin.Forms.Color)App.Current.Resources["Primary"]).ToUIColor();
    }

    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 = Element.GetType();
            ((IEntryController)Element).SendCompleted();
        });
        toolbar.Items = new UIBarButtonItem[] {
            new UIBarButtonItem (UIBarButtonSystemItem.FlexibleSpace),
            doneButton
        };
        toolbar.TintColor = ((Xamarin.Forms.Color)App.Current.Resources["Primary"]).ToUIColor();
        Control.TextField.InputAccessoryView = toolbar;
    }
    private void SetReturnType(ReturnType type)
    {
        switch (type)
        {
            case ReturnType.Go:
                Control.TextField.ReturnKeyType = UIReturnKeyType.Go;
                break;
            case ReturnType.Next:
                Control.TextField.ReturnKeyType = UIReturnKeyType.Next;
                break;
            case ReturnType.Send:
                Control.TextField.ReturnKeyType = UIReturnKeyType.Send;
                break;
            case ReturnType.Search:
                Control.TextField.ReturnKeyType = UIReturnKeyType.Search;
                break;
            case ReturnType.Done:
                Control.TextField.ReturnKeyType = UIReturnKeyType.Done;
                break;
            default:
                Control.TextField.ReturnKeyType = UIReturnKeyType.Default;
                break;
        }
    }
}

2 Replies

AJ AhamedAliNishad JahirHussain Syncfusion Team January 24, 2024 02:30 PM UTC

Hi Bryson,

 

Currently, we are checking the reported query and we will let you know the details on or before January 29th, 2023. We appreciate your patience until then.

 

Regards,

Ahamed Ali Nishad.



AJ AhamedAliNishad JahirHussain Syncfusion Team January 29, 2024 02:10 PM UTC

Hi Bryson,


Query: How can I successfully get my custom done button to close the SfAutoComplete Keyboard on iOS?


We have reviewed your query, and based on the provided information, we have created a sample. In this sample, we have created a custom class that inherits from SfAutoComplete, and we have implemented a CustomRenderer specifically for iOS. Within this renderer, we added code to display a custom "Done" button at the top of the keyboard. Additionally, we included code to handle the keyboard closing when the custom "Done" button is pressed, as demonstrated in the following code snippet. For your convenience, we have attached the sample and a video for reference. Kindly review them and let us know the details.


Code Snippet :


[assembly: ExportRenderer(typeof(CustomAutoComplete), typeof(CustomAutoCompleteRenderer))]

namespace SfAutoCompleteXamarin.iOS

{

    public class CustomAutoCompleteRenderer : SfAutoCompleteRenderer

    {

        protected override void OnElementChanged(ElementChangedEventArgs<SfAutoComplete> e)

        {

            base.OnElementChanged(e);

            if (Control != null && e.NewElement != null)

            {

 

                var toolbar = new UIToolbar(new CoreGraphics.CGRect(0, 0, Control.Frame.Size.Width, 44))

                { BarStyle = UIBarStyle.Default, Translucent = true };

                var doneButton = new UIBarButtonItem("Done", UIBarButtonItemStyle.Done, (sender, args) => { HandleShouldReturn(Control.TextField); });

                var flexibleSpace = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace);

                toolbar.Items = new[] { flexibleSpace, doneButton };

 

                Control.TextField.InputAccessoryView = toolbar;

                Control.TextField.ReturnKeyType = UIReturnKeyType.Done;

              

            }

 

        }

 

        private void HandleShouldReturn(UITextField tf)

        {

            if (tf.ReturnKeyType == UIReturnKeyType.Done)

            {

                tf.ResignFirstResponder();

            }

        }

    }

 

     

}


Regards,

Ahamed Ali Nishad.


Attachment: SfAutoCompleteDoneButton_10feca75.zip

Loader.
Up arrow icon