How to change background color of UIButton in Mono

2019-01-29 01:09发布

问题:

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.

回答1:

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


回答2:

Make sure your code is actually running in your eventHandler by adding a breakpoint or a console-output. If this works out, maybe you're just missing an "SetNeedsDisplay".

Try this:

private void buttonChangeColor_TouchUpInside (object sender, EventArgs e){
  if (this.isRed) {
    Console.WriteLine("isRed"); //Debug-Output
    this.View.BackgroundColor = UIColor.LightGray;
    this.isRed = false;
  } else {
    Console.WriteLine("isNotRed"); // Debug-Output
    this.View.BackgroundColor = UIColor.Red;
    this.isRed = true; 
  }
  this.View.SetNeedsDisplay(); // Should force the View to redraw
}


回答3:

This example was taken from one of the MonoTouch books and I had to convert the source code to work on the latest release for MonoTouch.

The original source was referring subView as

this.subView.BackgroundColor = UIColor.LightGray;

And in the process I failed to create an outlet for the view, i.e subView.

If there is no outlet created, the view can be accessed in the following fashion also

this.View.SubViews[0].BackgroundColor = UIColor.LightGray 

and it fixed the problem.



标签: xamarin.ios