Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

MaterialEntryField InputType numeric iOS #115

Closed
thiagoparabas opened this issue Mar 11, 2019 · 5 comments
Closed

MaterialEntryField InputType numeric iOS #115

thiagoparabas opened this issue Mar 11, 2019 · 5 comments
Labels
enhancement New feature or request valid This issue/bug/feature request is valid

Comments

@thiagoparabas
Copy link

When selected InputType = Numeric in iOS not ReturnButton or Ok button.

@contrix09
Copy link

Hi, native implementation of the UITextField in iOS does not have a return button when the input type is Numeric.

@thiagoparabas
Copy link
Author

@contrix09

how do I get the ReturnCommand to work with numeric?

@thiagoparabas
Copy link
Author

thiagoparabas commented Mar 13, 2019

using System.ComponentModel;
using System.Drawing;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using XF.Material.Forms.UI.Internals;
using XF.Material.iOS.Renderers.Internals;

[assembly: ExportRenderer(typeof(MaterialEntry), typeof(MaterialEntryRenderer))]

namespace XF.Material.iOS.Renderers.Internals
{
    internal class MaterialEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (e?.NewElement != null)
            {
                this.SetControl();
            }

            if (Element == null)
                return;

            if (Element.Keyboard == Keyboard.Numeric)
                AddDoneButton();
        }

        private void SetControl()
        {
            if (this.Control == null)
            {
                return;
            }

            var heightConstraint =
                NSLayoutConstraint.Create(this.Control, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1, 20f);
            this.Control.AddConstraint(heightConstraint);
            this.Control.BackgroundColor = UIColor.Clear;
            this.Control.TintColor = (this.Element as MaterialEntry)?.TintColor.ToUIColor();
            this.Control.BorderStyle = UITextBorderStyle.None;
            this.Control.TranslatesAutoresizingMaskIntoConstraints = false;
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (this.Control == null) return;

            if (e?.PropertyName == nameof(MaterialEntry.TintColor))
            {
                this.Control.TintColor = (this.Element as MaterialEntry)?.TintColor.ToUIColor();
            }
        }

        /*
            Based on example from: https://forums.xamarin.com/discussion/18346/add-done-button-to-keyboard-on-ios
        */

        /// <summary>
        /// <para>Add toolbar with Done button</para>
        /// </summary>
        protected void AddDoneButton()
        {
            var toolbar = new UIToolbar(new RectangleF(0.0f, 0.0f, 50.0f, 44.0f));

            var doneButton = new UIBarButtonItem(UIBarButtonSystemItem.Done, delegate
            {
                Control.ResignFirstResponder();
                ((IEntryController)Element).SendCompleted();
            });

            toolbar.Items = new[] { new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace), doneButton };
            this.Control.InputAccessoryView = toolbar;
        }
    }
}

@thiagoparabas
Copy link
Author

@contrix09

I can not open PR is returning error 403.

@contrix09
Copy link

Ok. I will apply this.

@contrix09 contrix09 self-assigned this Mar 14, 2019
@contrix09 contrix09 added enhancement New feature or request valid This issue/bug/feature request is valid labels Mar 14, 2019
@contrix09 contrix09 removed their assignment Sep 30, 2019
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request valid This issue/bug/feature request is valid
Projects
None yet
Development

No branches or pull requests

1 participant