Toran Billups
Toran Billups

Reputation: 27399

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?

Upvotes: 8

Views: 8970

Answers (4)

130nk3r5
130nk3r5

Reputation: 1628

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

Upvotes: 3

Duncan C
Duncan C

Reputation: 131426

I just discovered a solution that works perfectly, and I have no idea why.

If you select the button in Interface Builder, and open the "identity inspector", you can add "User Defined Runtime Attributes". If you add a key of "self.fillColor", with a type of color, that color is used as the fill color for the button.

I discovered this purely by accident. I had a custom subclass of UIButton called ColoredButton where I drew the rounded rectangle and filled it myself. It used a fillColor property. I changed one of my ColoredButton objects back to UIButton to test something, and was surprised to find that it was still filled with a color.

I don't know why it works, but it does.

Upvotes: 2

Jacob Jennings
Jacob Jennings

Reputation: 2836

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;

Upvotes: 11

tux91
tux91

Reputation: 1684

Use setImage:forState:

- (void)setImage:(UIImage *)image forState:(UIControlState)state

Also, don't forget to create your button with [UIButton buttonWithType:UIButtonTypeCustom]

Upvotes: 0

Related Questions