Ios 将边框颜色从按钮式文本颜色更改为';s已启用/禁用

Ios 将边框颜色从按钮式文本颜色更改为';s已启用/禁用,ios,xamarin,uibutton,xamarin.ios,uicolor,Ios,Xamarin,Uibutton,Xamarin.ios,Uicolor,如果“我的文本”字段为空,则应禁用该按钮,按钮中的textColor和borderColor应为灰色。但当按钮被启用时,颜色应该是蓝色,例如 更改文本颜色很容易: button.SetTitleColor(UIColor.Black, UIControlState.Normal); button.SetTitleColor (UIColor.Gray, UIControlState.Disabled); 但是如何更改边框的颜色呢?试试下面的代码 var button = UIButton.F

如果“我的文本”字段为空,则应禁用该按钮,按钮中的textColor和borderColor应为灰色。但当按钮被启用时,颜色应该是蓝色,例如

更改文本颜色很容易:

button.SetTitleColor(UIColor.Black, UIControlState.Normal);
button.SetTitleColor (UIColor.Gray, UIControlState.Disabled);
但是如何更改边框的颜色呢?

试试下面的代码

 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;
希望它对您的
ViewController
ViewDidLoad()
方法有所帮助:

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();
};
更新方法:

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;
    }
}
残疾人士:

启用:


您也可以使用KeyValueObserving(KVO)来执行此操作,如下所示:

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

TBH只是想看看是否可以这样做,我在XamarinCool中没有做过很多KVO,也从未真正看到过在C#中做过。当提到KVO时,我通常运行到最接近的条,几年前我继承了一个项目,拥有近千名活动观察员(ARC前),只是疯狂的编码:-/
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.
    }
}