Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to change a UIButtons background color?

This one has me stumped.

Is it possible at all to change the background color of a UIButton in Cocoa for iPhone. I've tried setting the background color but it only changes the corners. setBackgroundColor: seems to be the only method available for such things.

[random setBackgroundColor:[UIColor blueColor]]; [random.titleLabel setBackgroundColor:[UIColor blueColor]]; 
like image 965
dubbeat Avatar asked May 11 '10 07:05

dubbeat


People also ask

How to change background colour of a button swift?

Add a button on your storyboard, select it Go to it's attribute inspector and select 'Background' property to choose the color.

Can I change the background color of a website?

Change the Background Color of a Web Page Open and display the Web page you want to use. Right-click the page to which you want to change a background color, and then click Page Properties. Click the Formatting tab. Click the Background list arrow.

How do you change the background color on a storyboard?

First let us see using storyboard, Open Main. storyboard and add one view to the View Controller. On the right pane you can see the property, and from there update the background color to color you want your view to be as show below.

How do I change the button title color in Swift?

Is-it possible to change the color of the Title for the button ? Usually, use setTitleColor(_:for:) because buttons can have different UIControlState : . normal , .


2 Answers

This can be done programmatically by making a replica:

loginButton = [UIButton buttonWithType:UIButtonTypeCustom]; [loginButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; loginButton.backgroundColor = [UIColor whiteColor]; loginButton.layer.borderColor = [UIColor blackColor].CGColor; loginButton.layer.borderWidth = 0.5f; loginButton.layer.cornerRadius = 10.0f; 

edit: of course, you'd have to #import <QuartzCore/QuartzCore.h>

edit: to all new readers, you should also consider a few options added as "another possibility". for you consideration.

As this is an old answer, I strongly recommend reading comments for troubleshooting

like image 179
Stian Storrvik Avatar answered Sep 20 '22 23:09

Stian Storrvik


I have a different approach,

[btFind setTitle:NSLocalizedString(@"Find", @"") forState:UIControlStateNormal];     [btFind setBackgroundImage:[CommonUIUtility imageFromColor:[UIColor cyanColor]]          forState:UIControlStateNormal]; btFind.layer.cornerRadius = 8.0; btFind.layer.masksToBounds = YES; btFind.layer.borderColor = [UIColor lightGrayColor].CGColor; btFind.layer.borderWidth = 1; 

From CommonUIUtility,

+ (UIImage *) imageFromColor:(UIColor *)color {     CGRect rect = CGRectMake(0, 0, 1, 1);     UIGraphicsBeginImageContext(rect.size);     CGContextRef context = UIGraphicsGetCurrentContext();     CGContextSetFillColorWithColor(context, [color CGColor]);     //  [[UIColor colorWithRed:222./255 green:227./255 blue: 229./255 alpha:1] CGColor]) ;     CGContextFillRect(context, rect);     UIImage *img = UIGraphicsGetImageFromCurrentImageContext();     UIGraphicsEndImageContext();     return img; } 

Don't forget to #import <QuartzCore/QuartzCore.h>

like image 30
karim Avatar answered Sep 18 '22 23:09

karim