Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

xamarin.ios - How to change background color of UIButton in Monotouch

I am newbie to Monotouch and have added the following code to my SingleViewApplication. And you can see my controller code below, where I am trying to change the background after click of the button.

public partial class FirstViewAppViewController : UIViewController
{
    public FirstViewAppViewController () : base ("FirstViewAppViewController", null)
    {
    }

    UIButton buttonChangeColor;
    private void CreateButton (){
      RectangleF viewFrame = this.View.Frame;
      RectangleF buttonFrame = new RectangleF (10f, viewFrame.Bottom -  
        200f, viewFrame.Width - 20f, 50f);
      this.buttonChangeColor = UIButton.FromType  
        (UIButtonType.RoundedRect);
      this.buttonChangeColor.Frame = buttonFrame;
      this.buttonChangeColor.SetTitle ("Tap to change view color",  
        UIControlState.Normal);
      this.buttonChangeColor.SetTitle ("Changing color...",  
        UIControlState.Highlighted);
      this.buttonChangeColor.TouchUpInside +=  
        this.buttonChangeColor_TouchUpInside;
      this.View.AddSubview (this.buttonChangeColor);
    }

    bool isRed;
    private void buttonChangeColor_TouchUpInside (object sender, EventArgs e){
      if (this.isRed) {
        this.View.BackgroundColor = UIColor.LightGray;
        this.isRed = false;
      } else {
        this.View.BackgroundColor = UIColor.Red;
        this.isRed = true; 
      }
    }

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

        this.CreateButton();

        // Perform any additional setup after loading the view, typically from a nib.
    }

It looks fine to me, but I dont see the background color changing in simulator.

Any ideas? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am also face this problem but i solve this issue hope this code is help you

public static class SwapUIButtonColor
{
    #region prop
    public static UIButton PreviousButton {
    get
    {
        return _previousButton
    }
    set
    {
        _previousButton = value;
    }
}
public static UIButton CurrentButton 
{
    get
    {
        return _currentButton;
    }
    set
    {
        _currentButton = value;
    }
}
public static bool SwapColor (UIButton sender, bool isPrevBakGraound)
{
    if (isPrevBakGraound == false)
    {
        InitApplyColorBorder (sender);
        SwapUIButtonColor.PreviousButton = sender;
        return true;
    }
    else
    {
        if (SwapUIButtonColor.PreviousButton != sender)
        {
            InitApplyColorBorder (sender);
            SwapUIButtonColor.CurrentButton = PreviousButton;
            PreviousButton = sender;
            SwapUIButtonColor.CurrentButton.Layer.BorderColor = (UIColor.Black).CGColor;
            SwapUIButtonColor.CurrentButton.Layer.BorderWidth = 0f;
            SwapUIButtonColor.CurrentButton.Layer.CornerRadius = 0f;    
        }
        else if (SwapUIButtonColor.PreviousButton == sender)
        {
            InitApplyColorBorder (sender);
            SwapUIButtonColor.PreviousButton = sender;  
        }
        return true;
    }
}
static void InitApplyColorBorder (UIButton sender)
{
    sender.Layer.BorderColor = (UIColor.Red).CGColor;
    sender.Layer.BorderWidth = 1.0f;
    sender.Layer.CornerRadius = 10f;
}

Also from ViewDidload Method Call this above method on UIButton Click events

private bool isPrevBakGraound = false;  
isPrevBakGraound = SwapUIButtonColor.SwapColor (btnReset, isPrevBakGraound);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...