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:
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.
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.