MAUI iOS - HideSoftInputOnTapped is not working issue

Aneetha 65 Reputation points
2024-09-24T09:29:56.11+00:00

I have an MAUI .NET 8 project, I am facing an issue that When I click outside the custom entry when it has focus, it won't lose focus and won't hide the soft keyboard even when I Set HideSoftInputOnTapped="True".

But this issue does not happen if CustomRender is not implemented.

Please help me to resolve this issue.

Please find the code

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:custom="clr-namespace:EntryPOC" HideSoftInputOnTapped="True"
             x:Class="EntryPOC.MainPage">

    <ScrollView>
        <VerticalStackLayout   x:Name="layout" 
            Padding="30,0"
            Spacing="25">
            
          <custom:CustomEntry Unfocused="VisualElement_OnUnfocused"            Completed="CustomEntry_OnCompleted"></custom:CustomEntry>
            
        </VerticalStackLayout>
    </ScrollView>

</ContentPage>

public class CustomEntry   : Entry
{
    public new event EventHandler Completed;

    public CustomEntry()
    {
        // Default parameter
    }

    public CustomEntry(string PropertyID)
    {
        ClassId = PropertyID;
        BackgroundColor = Colors.Transparent;
        HorizontalOptions = LayoutOptions.FillAndExpand;
    }

    public const string ReturnKeyPropertyName = "ReturnKeyType";


    public static readonly BindableProperty ReturnKeyTypeProperty = BindableProperty.Create(
        propertyName: ReturnKeyPropertyName,
        returnType: typeof(ReturnKeyTypes),
        declaringType: typeof(CustomEntry),
        defaultValue: ReturnKeyTypes.Done);

    public ReturnKeyTypes ReturnKeyType
    {
        get { return (ReturnKeyTypes)GetValue(ReturnKeyTypeProperty); }
        set { SetValue(ReturnKeyTypeProperty, value); }
    }

    public Constants.eKeyboardTypes KeyBoardType
    {
        get;
        set;
    }

    public void InvokeCompleted()
    {
        if (this.Completed != null)
            this.Completed.Invoke(this, null);
    }

    public void SetVisible()
    {
        Enable();
        IsVisible = true;
    }

    public void Enable()
    {
        IsEnabled = true;
        BackgroundColor = Colors.Transparent;
    }

    public void ReadOnly()
    {
        SetVisible();
        IsEnabled = false;
        BackgroundColor = Constants.ReadOnlyBackGroundColor;
    }

    public void Disabled()
    {
        SetVisible();
        IsEnabled = false;
        BackgroundColor = Constants.DisabledBackGroundColor;
    }

}

public enum ReturnKeyTypes : int
{
    Default,
    Go,
    Google,
    Join,
    Next,
    Route,
    Search,
    Send,
    Yahoo,
    Done,
    EmergencyCall,
    Continue
}

public class CustomEntry_IOS: EntryRenderer
{
        public CustomEntry_IOS()
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            CustomEntry entry = (CustomEntry)this.Element;
            base.OnElementChanged(e);
            if ((Control != null) && (e.NewElement != null))
                Control.ReturnKeyType = (e.NewElement as CustomEntry).ReturnKeyType.GetValueFromDescription();
            Control.Placeholder = entry.Placeholder;
            //Control?.ResignFirstResponder();
            // Editor Action is called when the return button is pressed

            //if (entry != null && entry.Keyboard == Keyboard.Numeric)
            //{
            //    Control.KeyboardType = UIKeyboardType.NumbersAndPunctuation;
            //}
            if (Control != null)
                Control.ShouldReturn += (UITextField tf) =>
                {
                    entry.InvokeCompleted();
                    return true;
                };
            if (entry != null)
            {
                SetKeybordType(entry.KeyBoardType);
            }
        }






        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            try
            {
                base.OnElementPropertyChanged(sender, e);
               // Control?.ResignFirstResponder();
                if (e.PropertyName == CustomEntry.ReturnKeyPropertyName)
                {

                    Control.ReturnKeyType = (sender as CustomEntry).ReturnKeyType.GetValueFromDescription();

                    //Control.ReturnKeyType = UIReturnKeyType.Continue;
                }
                if ((sender as CustomEntry) != null && (sender as CustomEntry).Keyboard == Keyboard.Numeric)
                {
                    Control.KeyboardType = UIKeyboardType.PhonePad;
                }
            }
            catch (Exception ex)
            {
              //  AppDelegate.log?.WriteLog(ex, Location: this.GetType().Name + "." + MethodBase.GetCurrentMethod().Name);
            }

        }

        void SetKeybordType(Constants.eKeyboardTypes keyboardType)
        {
            switch (keyboardType)
            {
                case Constants.eKeyboardTypes.Default:
                    this.Control.KeyboardType = UIKeyboardType.Default;
                    break;
                case Constants.eKeyboardTypes.Email:
                    this.Control.KeyboardType = UIKeyboardType.EmailAddress;
                    break;
                case Constants.eKeyboardTypes.Numeric:
                    this.Control.KeyboardType = UIKeyboardType.NumberPad;
                    break;
                case Constants.eKeyboardTypes.NumericWithDecimal:
                    this.Control.KeyboardType = UIKeyboardType.DecimalPad;
                    break;
                case Constants.eKeyboardTypes.NumericWithMinusAndDigit:
                case Constants.eKeyboardTypes.NumericWithMinus:
                    this.Control.KeyboardType = UIKeyboardType.NumbersAndPunctuation;
                    break;
                default:
                    this.Control.KeyboardType = UIKeyboardType.Default;
                    break;
            }
        }
    }

    public static class EnumExtensions
    {
        public static UIReturnKeyType GetValueFromDescription(this ReturnKeyTypes value)
        {
            var type = typeof(UIReturnKeyType);
            if (!type.IsEnum) throw new InvalidOperationException();
            foreach (var field in type.GetFields())
            {
                var attribute = Attribute.GetCustomAttribute(field,
                    typeof(DescriptionAttribute)) as DescriptionAttribute;
                if (attribute != null)
                {
                    if (attribute.Description == value.ToString())
                        return (UIReturnKeyType)field.GetValue(null);
                }
                else
                {
                    if (field.Name == value.ToString())
                        return (UIReturnKeyType)field.GetValue(null);
                }
            }
            throw new NotSupportedException($"Not supported on iOS: {value}");
        }
    }

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,505 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.