3
votes

I have an Entry in Xamarin Forms and for Android, its displayed with an underline by default and when I test it for iOS it's always with a rectangle like this:

enter image description here

And I want it to be like just like in Android:

enter image description here

I used Custom render where I specified that I want an underline but its still displayed in a rectangle. This code doesn't work:

class MyEntryRenderer : EntryRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {             
            Control.BorderStyle = UITextBorderStyle.Line;                
        }
    }
}

I have solved this problem with the next code:

class MyEntryRenderer : EntryRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {             
            Control.BorderStyle = UITextBorderStyle.None;

            //Create borders(bottom only)
            CALayer border = new CALayer();
            float width = 2.0f;               
            border.BorderColor = Color.FromHex(ColorConstants.BlueHex).ToCGColor();
            border.Frame = new CGRect( 0, 40, 400, 2.0f);
            border.BorderWidth = width;

            Control.Layer.AddSublayer(border);
            Control.Layer.MasksToBounds = true;
        }
    }
}

But still, don't understand why the previous code doesn't work, it should be simple as that. Any ideas?

1

1 Answers

1
votes

https://developer.xamarin.com/api/type/UIKit.UITextBorderStyle/

The enum values for UITextBorderStyle are Bezel, Line, None, and RoundedRect. All of them (except for None) simply describe the style of the border that goes around the entire UITextView, so the Line value doesn't mean a single line, it means a solid line rectangle around the entire view, as opposed to a Bezel or rounded rectangle.