Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the color of a UIButton inside (not background color)

I'm tweaking some visual changes and noticed that when I try to set the background color of a UIButton it only sets the color outside the actual button (not inside the button itself)

UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(6, 9, 50, 25);    
[button setTitle:@"Select" forState:UIControlStateNormal];
button.backgroundColor = [UIColor redColor]; 

Does another property exist that would allow me to set the color?

like image 220
Toran Billups Avatar asked Jan 22 '11 04:01

Toran Billups


People also ask

How do you change BackColor buttons?

To change the background color of the button, use the CSS background-color property and give it a value of a color of your taste. In the . button selector, you use background-color:#0a0a23; to change the background color of the button.

How do you change the color of a button in typescript?

In the ChangeColor() function, we call another function Change2(). This function is used to change the color of the button again.


2 Answers

colored UIButton

If you are not wanting to use images, and want it to look exactly like the Rounded Rect style, try this. Just place a UIView over the UIButton, with an identical frame and auto resize mask, set the alpha to 0.3, and set the background to a color. Then use the snippet below to clip the rounded edges off the colored overlay view. Also, uncheck the 'User Interaction Enabled' checkbox in IB on the UIView to allow touch events to cascade down to the UIButton underneath.

One side effect is that your text will also be colorized.

#import <QuartzCore/QuartzCore.h>

colorizeOverlayView.layer.cornerRadius = 10.0f;
colorizeOverlayView.layer.masksToBounds = YES;
like image 57
Jacob Jennings Avatar answered Nov 04 '22 09:11

Jacob Jennings


For Xamarin

var addNote = new UIButton(UIButtonType.Custom);
        //addNote.SetImage(new UIImage("Images/11-x.png"), UIControlState.Normal);
        addNote.SetTitle("Add Note", UIControlState.Normal);
        addNote.Frame = new RectangleF(0, 0, 130, 30);
        addNote.SetTitleColor(UIColor.Blue, UIControlState.Normal);
        addNote.BackgroundColor = UIColor.LightGray;
        addNote.Layer.CornerRadius = 10f;
        addNote.Layer.BorderColor = new MonoTouch.CoreGraphics.CGColor(0f, 100f, 0f);
        addNote.Layer.BorderWidth = 2f;
        addNote.TouchUpInside += delegate
        {
            AddNote();
        };
like image 44
130nk3r5 Avatar answered Nov 04 '22 09:11

130nk3r5