Forum Xamarin Xamarin.Forms

How to do an Entry for IOS to be the same as the Android Entry Style.

ShettyAshShettyAsh Member ✭✭✭

Hi,
I want to customise entry style of ios to android style..Please help

Thanks

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    You can use custom renderer ,but which android style do you want? Can you attach the image to elaborate?

  • ShantimohanElchuriShantimohanElchuri USMember ✭✭✭✭✭

    @AshwiniShetty16 said:
    Hi,
    I want to customise entry style of ios to android style..Please help

    Thanks

    Apple wants the Button be like a web link. Since Xamarin Forms conforms to native policies as far as possible the BackgroundColor is set to Transparent.

    You just set Backgroundcolor="Grey" and you will have a Button in iOS same as in Android.

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    @ShantimohanElchuri said:

    @AshwiniShetty16 said:
    Hi,
    I want to customise entry style of ios to android style..Please help

    Thanks

    Apple wants the Button be like a web link. Since Xamarin Forms conforms to native policies as far as possible the BackgroundColor is set to Transparent.

    You just set Backgroundcolor="Grey" and you will have a Button in iOS same as in Android.

    Or you can set a Style for your buttons to make them consistent across platforms. But as @ShantimohanElchuri says, Forms is designed 'out of the box' to conform to the native policy - so what you think is 'not nice' may be what your iOS-centric users are expecting.

  • LijuDanielLijuDaniel Member ✭✭
    edited March 17

    Hi @ShettyAsh, try this code to change iOS entry looks like native Android entry
    Add this renderer in your ios section
    Its working fine for me

    [assembly: ExportRenderer(typeof(Entry), typeof(CustomEntryRenderer))]
    namespace DemoApp.iOS.Utils
    {
    public class CustomEntryRenderer : EntryRenderer
    {
    private CALayer _borderLayer;
    protected override void OnElementChanged(ElementChangedEventArgs e)
    {
    base.OnElementChanged(e);

            if (Control == null)
                return;
    
            Control.BorderStyle = UITextBorderStyle.None;
    
            var element = Element as Entry;
            if (element == null)
                return;
    
            //DrawBorder(element.BorderColor.ToCGColor());
            DrawBorder(UIColor.FromRGB(156, 156, 156));
    
           e.NewElement.Unfocused += (sender, evt) =>
            {
                DrawBorder(UIColor.FromRGB(156, 156, 156)); // unfocused, set color
    
            };
            e.NewElement.Focused += (sender, evt) =>
            {
                DrawBorder(UIColor.FromRGB(245, 0, 47)); ; // focused, set color
            };
        }
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
    
            var element = Element as Entry;
            if (element == null)
                return;
    
            DrawBorder(UIColor.FromRGB(156, 156, 156));
        }
        public override CGRect Frame
        {
            get { return base.Frame; }
            set
            {
                base.Frame = value;
    
                var element = Element as Entry;
                if (element == null)
                    return;
    
                // DrawBorder(element.BorderColor.ToCGColor());
                DrawBorder(UIColor.FromRGB(156, 156, 156));
            }
        }
        private void DrawBorder(UIColor borderColor)
        {
            if (Frame.Height <= 0 || Frame.Width <= 0)
                return;
    
            if (_borderLayer == null)
            {
                _borderLayer = new CALayer
                {
                    MasksToBounds = true,
                    Frame = new CGRect(0f, Frame.Height - 1, Frame.Width, 1f),
                    BorderColor = borderColor.CGColor,
                    BorderWidth = 1.0f
                };
    
                Control.Layer.AddSublayer(_borderLayer);
                Control.BorderStyle = UITextBorderStyle.None;
            }
            else
            {
                _borderLayer.BorderColor = borderColor.CGColor;
                _borderLayer.Frame = new CGRect(0f, Frame.Height - 1, Frame.Width, 1f);
            }
        }
    }
    

    }

Sign In or Register to comment.