在启用/禁用TextColor时,从Button更改BorderColor

时间:2016-04-16 12:47:09

标签: ios xamarin uibutton xamarin.ios uicolor

如果我的TextField为空,则应禁用该按钮,并且该按钮的textColor和borderColor应为灰色。但是当启用该按钮时,颜色应为蓝色。

更改textColor非常简单:

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

但是如何更改边框的颜色?

4 个答案:

答案 0 :(得分:2)

使用ViewDidLoad ()的{​​{1}}方法:

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();
};

禁用:

enter image description here

启用:

enter image description here

答案 1 :(得分:0)

试试这段代码,

 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;

希望它有用

答案 2 :(得分:0)

你可以使用KeyValueObserving(KVO)这样做:

.res

答案 3 :(得分:0)

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.
    }
}