3

I'm having trouble programmatically hard coding a color in for a UIButton using MonoTouch. The standard RectRound buttons are white and I need this button to be blue. Here is my code:

     private void CreateButton() {
        RectangleF viewFrame = this.subView.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.SetTitleColor (UIColor.Red, UIControlState.Normal);
 //This here is my attempt. The rounded rectangle remains white and I can see some blue peeking out from behind but I need the button to be blue, not the background
        this.buttonChangeColor.BackgroundColor = UIColor.Blue; 
        this.buttonChangeColor.TouchUpInside += this.buttonChangeColor_TouchUpInside;
        this.subView.AddSubview (this.buttonChangeColor);

    }

Thanks a lot for the help!

poupou
  • 43,413
  • 6
  • 77
  • 174
KrispyK
  • 235
  • 6
  • 18

2 Answers2

2

Have a look at Is it even possible to change a UIButtons background color?

It explains why, for UIButtonType.RoundedRect, you won't be able to change the background color as you would expect. I.e. You'll need to create a Custom button and duplicate the rounded-rect look.

Community
  • 1
  • 1
poupou
  • 43,413
  • 6
  • 77
  • 174
1

I have done this in my previos anwser to post so check link that i paste change UIButton Color dynamically

Community
  • 1
  • 1
IMMORTAL
  • 2,707
  • 3
  • 21
  • 37