ChrKahl
ChrKahl

Reputation: 651

Changing BorderColor from Button like TextColor when it's enabled/disabled

If my TextField is empty, the button should be disabled and the textColor and borderColor from the button should be gray. But when the button is enabled the colors should be blue for example.

Changing the textColor is easy:

button.SetTitleColor(UIColor.Black, UIControlState.Normal);
button.SetTitleColor (UIColor.Gray, UIControlState.Disabled);

But how can I change the color from the border?

Upvotes: 1

Views: 1289

Answers (4)

Alvin George
Alvin George

Reputation: 14296

public partial class ViewController : UIViewController
{
    UIButton sampleButton;

    public ViewController (IntPtr handle) : base (handle)
    {
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        // Perform any additional setup after loading the view, typically from a nib.
        sampleButton = new UIButton (new CoreGraphics.CGRect (10f, 100f, 300f, 50f));
        this.sampleButton.SetTitle ("Sample Button", UIControlState.Normal);
        this.sampleButton.SetTitleColor (UIColor.Blue, UIControlState.Normal);
        this.sampleButton.Layer.BorderWidth = 3;
        this.sampleButton.Layer.CornerRadius = 3;
        this.sampleButton.BackgroundColor = UIColor.Yellow;
        this.sampleButton.TouchUpInside += sampleButtonClicked;
    }

    public override void ViewDidAppear (bool animated)
    {
        base.ViewDidAppear (animated);
        this.View.AddSubview (sampleButton);        
    }

    private void sampleButtonClicked (object sender, EventArgs e)
    {
        //Make this variable change and boarder color being managed by a switch loop
        var selectColorCode = 1;

        switch (selectColorCode) {

            case 0:
                this.sampleButton.Enabled = false;
                this.sampleButton.Layer.BorderColor = UIColor.Brown.CGColor;
                break;

            case 1:
                this.sampleButton.Enabled = true;
                this.sampleButton.Layer.BorderColor = UIColor.Red.CGColor;
                break;

            default:
                break;
        }

    }

    public override void DidReceiveMemoryWarning ()
    {
        base.DidReceiveMemoryWarning ();
        // Release any cached data, images, etc that aren't in use.
    }
}

Upvotes: 0

Iain Smith
Iain Smith

Reputation: 9703

You can do it with KeyValueObserving (KVO) too like so:

    IntPtr tokenObserveButtonEnabled = (IntPtr) 1;
    UIButton button;

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        // Perform any additional setup after loading the view, typically from a nib.

        var textf = new UITextField (new CGRect (150, 150, 50, 50));

        button = new UIButton (new CGRect (50, 50, 50, 50));
        button.SetTitle ("TEST", UIControlState.Normal);
        button.SetTitleColor(UIColor.Black, UIControlState.Normal);
        button.SetTitleColor (UIColor.Gray, UIControlState.Disabled);
        button.Layer.BorderWidth = 1;
        button.Layer.CornerRadius = 5;
        button.AddObserver (this, (NSString)"enabled",
            NSKeyValueObservingOptions.Old | NSKeyValueObservingOptions.New, tokenObserveButtonEnabled);

        Add (button);
        Add (textf);
        textf.AllEditingEvents += (object sender, EventArgs e) => 
        {
            var tf = sender as UITextField;
            button.Enabled = tf.HasText;
        };
    }

    public override void ObserveValue (NSString keyPath, NSObject ofObject, NSDictionary change, IntPtr ctx)
    {
        if (ctx == tokenObserveButtonEnabled) {
            button.Layer.BorderColor = button.Enabled ? UIColor.Black.CGColor : UIColor.Gray.CGColor ;
        }
    }

Upvotes: 0

SushiHangover
SushiHangover

Reputation: 74164

In the ViewDidLoad () method of your ViewController:

MyButton.SetTitleColor(UIColor.Black, UIControlState.Normal);
MyButton.SetTitleColor (UIColor.Gray, UIControlState.Disabled);
MyButton.Layer.BorderWidth = 2;
MyButton.Layer.CornerRadius = 2;
UpdateButton();
MyEntryField.AllEditingEvents += (object sender, EventArgs e) => {
    UpdateButton();
};

Update Method:

public void UpdateButton() {
    if (MyEntryField.HasText) {
        MyButton.Enabled = false;
        MyButton.Layer.BorderColor = UIColor.Blue.CGColor;
    } else {
        MyButton.Enabled = true;
        MyButton.Layer.BorderColor = UIColor.DarkGray.CGColor;
    }
}

Disabled:

enter image description here

Enabled:

enter image description here

Upvotes: 2

Iyyappan Ravi
Iyyappan Ravi

Reputation: 3245

try this code,

 var button = UIButton.FromType(UIButtonType.System); // New type in iOS 7
  button.Layer.BorderWidth = 1;
  button.Layer.CornerRadius = 4;
  button.Layer.BorderColor = UIColor.Black.CGColor;

hope its helpful

Upvotes: 0

Related Questions