iPhone : How to set BackgroundColor of UIButton with buttonType UIButtonTypeCustom iPhone : How to set BackgroundColor of UIButton with buttonType UIButtonTypeCustom xcode xcode

iPhone : How to set BackgroundColor of UIButton with buttonType UIButtonTypeCustom


You could create an image programmatically and so have the ability to use your colors in a dynamic way:

Create a category for UIButton with this method and be sure to have QuartzCore lib imported via @import QuartzCore:

- (void)setColor:(UIColor *)color forState:(UIControlState)state{    UIView *colorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];    colorView.backgroundColor = color;    UIGraphicsBeginImageContext(colorView.bounds.size);    [colorView.layer renderInContext:UIGraphicsGetCurrentContext()];    UIImage *colorImage = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    [self setBackgroundImage:colorImage forState:state];}

This is gonna create an image with the size of your button for your specified color and then assigning it for the wished state. Because of the usage of the backgroundImage you can still set a title for the button via the setTitle:forState: method.


This is what I do ... it's a custom subclass of a UIButton, just set the normal and highlighted colors and everything shpould work fine ;)

Header file:

////  FTCustomButton.h//  FTLibrary////  Created by Ondrej Rafaj on 14/01/2013.//  Copyright (c) 2013 Fuerte International. All rights reserved.//#import <UIKit/UIKit.h>@interface FTCustomButton : UIButton@property (nonatomic, strong, readonly) UIColor *normalColor;@property (nonatomic, strong, readonly) UIColor *highlightedColor;- (void)setNormalColor:(UIColor *)normalColor;- (void)setHighlightedColor:(UIColor *)highlightedColor;@end

This is the implementation file:

////  FTCustomButton.m//  FTLibrary////  Created by Ondrej Rafaj on 14/01/2013.//  Copyright (c) 2013 Fuerte International. All rights reserved.//#import "FTCustomButton.h"//*****************************************************************************// private interface declaration//*****************************************************************************@interface MCSelectionButton ()    @property(nonatomic, strong, readwrite) UIColor *normalColor;    @property(nonatomic, strong, readwrite) UIColor *highlightedColor;@end//*****************************************************************************// public interface implementation//*****************************************************************************@implementation FTCustomButton#pragma mark Settings- (void)setNormalColor:(UIColor *)normalColor {    [self setBackgroundColor:normalColor];    _normalColor = normalColor;}- (void)setHighlightedColor:(UIColor *)highlightedColor {    _highlightedColor = highlightedColor;}#pragma mark Actions- (void)didTapButtonForHighlight:(UIButton *)sender {    [self setBackgroundColor:_highlightedColor];}- (void)didUnTapButtonForHighlight:(UIButton *)sender {    [self setBackgroundColor:_normalColor];}#pragma mark Initialization- (void)setupButton {    [self addTarget:self action:@selector(didTapButtonForHighlight:) forControlEvents:UIControlEventTouchDown];    [self addTarget:self action:@selector(didUnTapButtonForHighlight:) forControlEvents:UIControlEventTouchUpInside];    [self addTarget:self action:@selector(didUnTapButtonForHighlight:) forControlEvents:UIControlEventTouchUpOutside];}- (id)init {    self = [super init];    if (self) {        [self setupButton];    }    return self;}- (id)initWithCoder:(NSCoder *)aDecoder {    self = [super initWithCoder:aDecoder];    if (self) {        [self setupButton];    }    return self;}- (id)initWithFrame:(CGRect)frame {    self = [super initWithFrame:frame];    if (self) {        [self setupButton];    }    return self;}@end


Make sure the button type is custom as someone else already stated. Then doing the following will bring back those rounded corners:

 [self.myButton.layer setCornerRadius:8.0f]; [self.myButton.layer setMasksToBounds:YES]; [self.myButton.layer setBorderWidth:1.0f]; [self.myButton.layer setBorderColor:[[UIColor whiteColor] CGColor]]; self.myButton.backgroundColor = [UIColor redColor];

Though, personally, I'm a big fan of gradient buttons over solid colors... also, background color doesn't have different states, whereas background images do:

 [self.myButton setBackgroundImage:[UIImage imageNamed:@"gradient.png"] forState:UIControlStateNormal];

Using an image, your button will darken with selection (or you could provide a different background image per state to do something different). This won't happen with background color, the background will always stay the same, only your button label changing per state.