Maurits van Beusekom
Maurits van Beusekom

Reputation: 5989

Xamarin.iOS change bordercolor for custom control

I am trying to create a custom control based on the UITextField. One of the requirements is to be able to change the border color of the control at designtime.

At the moment I have the following code (see below). At this point the custom control shows up in the designer and I can drag it onto on of my views. The designer property "BorderColor" also shows in the designer and I can select a color. However the border color of the control itself doesn't change (only the property value changes but the border of the control stays black).

[Register("RidderTextField"), DesignTimeVisible(true)]
public class RidderTextField
    : UITextField
{
    private UIColor _borderColor = UIColor.Orange;

    public RidderTextField(IntPtr handle) : base(handle) { }
    public RidderTextField(RectangleF frame) : base(frame) { }
    public RidderTextField()
    {
        Initialize();
    }

    [Export("BorderColor"), Browsable(true)]
    public UIColor BorderColor
    {
        get { return _borderColor; }
        set
        {
            _borderColor = value;
            SetNeedsDisplay();
        }
    }

    public override void AwakeFromNib()
    {
        base.AwakeFromNib();

        Initialize();
    }

    private void Initialize()
    {
        BackgroundColor = UIColor.White;
        BorderStyle = UITextBorderStyle.Line;
    }

    public override void Draw(CoreGraphics.CGRect rect)
    {
        base.Draw(rect);

        Layer.BorderColor = BorderColor.CGColor;
    }
}

Any help or guidance would be greatly appreciated.

Upvotes: 0

Views: 202

Answers (1)

Nerkyator
Nerkyator

Reputation: 3976

Just put

Layer.BorderWidth = 1.0f;

in Draw method

Upvotes: 2

Related Questions