How to change background color of UIButton in Monotouch

后端 未结 3 345
借酒劲吻你
借酒劲吻你 2020-12-22 02:20

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

相关标签:
3条回答
  • 2020-12-22 02:34

    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);
    
    0 讨论(0)
  • 2020-12-22 02:39

    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
    }
    
    0 讨论(0)
  • 2020-12-22 02:52

    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.

    0 讨论(0)
提交回复
热议问题