【问题标题】:How to change background color of UIButton in Monotouch如何在 Monotouch 中更改 UIButton 的背景颜色
【发布时间】:2018-08-10 21:35:25
【问题描述】:

我是 Monotouch 的新手,已将以下代码添加到我的 SingleViewApplication。您可以在下面看到我的控制器代码,我试图在单击按钮后更改背景。

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

对我来说看起来不错,但我没有看到模拟器中的背景颜色发生变化。

有什么想法吗?谢谢。

【问题讨论】:

    标签: xamarin.ios


    【解决方案1】:

    我也面临这个问题,但我解决了这个问题希望这段代码对你有帮助

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

    同样来自 ViewDidload 方法在 UIButton 点击​​事件上调用上述方法

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

    【讨论】:

      【解决方案2】:

      通过添加断点或控制台输出,确保您的代码实际在 eventHandler 中运行。如果这成功了,也许你只是缺少一个“SetNeedsDisplay”。

      试试这个:

      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
      }
      

      【讨论】:

      • 感谢 Chiffre 的回答。我可以使用断点调试代码并添加控制台语句,它们都按预期工作,但即使添加 SetNeedsDisplay(),背景颜色也不会改变。我已经在 Lion OS 上安装了最新版本的 MonoTouch(稳定)版本。
      【解决方案3】:

      这个例子取自一本 MonoTouch 书籍,我必须转换源代码才能在 MonoTouch 的最新版本上工作。

      原始来源将 subView 称为

      this.subView.BackgroundColor = UIColor.LightGray;
      

      在此过程中,我未能为视图创建一个出口,即 subView。

      如果没有创建出口,也可以通过以下方式访问视图

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

      它解决了问题。

      【讨论】:

        猜你喜欢
        • 2021-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-22
        • 1970-01-01
        • 2011-12-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多