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]];
Add a button on your storyboard, select it Go to it's attribute inspector and select 'Background' property to choose the color.
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.
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.
Is-it possible to change the color of the Title for the button ? Usually, use setTitleColor(_:for:) because buttons can have different UIControlState : . normal , .
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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With