How to set color inside UIButton (and not background color)

I tweak some visual changes and notice that when I try to set the background color of the UIButton, it 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]; 

Is there another property that would allow me to set the color?

+7
source share
4 answers

Use setImage:forState:

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

Also, be sure to create your own button using [UIButton buttonWithType:UIButtonTypeCustom]

0
source

colored UIButton

If you don't want to use the images and want it to look exactly like the Rounded Rect style, try this. Just place the UIView over the UIButton, with the same frame and auto-resize mask, set the alpha value to 0.3 and set the background to color. Then take the picture below to copy the rounded edges from the color overlay. Also, uncheck the "User Interaction" checkbox in IB on the UIView to allow cascading down events to the UIButton below it.

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

 #import <QuartzCore/QuartzCore.h> colorizeOverlayView.layer.cornerRadius = 10.0f; colorizeOverlayView.layer.masksToBounds = YES; 
+11
source

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

I just found a solution that works great, and I have no idea why.

If you select a button in Interface Builder and open the “Identity Inspector”, you can add “Custom Runtime Attributes”. If you add a "self.fillColor" key with a color type, this color is used as the fill color for the button.

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

I do not know why this works, but it is.

+2
source

All Articles