setBackgroundImage:forState:
Sets the background image to use for the specified button state.
Parameters
image
The background image to use for the specified state.
state
The state that uses the specified image. The values are described in UIControlState.
Discussion
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state. (setBackgroundImage:forState:)
Example for ( )
{
NSString *title = @"My button title";
UIColor *titleColor = [UIColor colorWithWhite:0.5f alpha:1.0f];
UIImage *bgImage = [UIImage imageNamed:@"yourImage.png"];
UIImage *highlightedBgImage = [UIImage imageNamed:@"yourImage.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// Create button.
//
button.bounds = CGRectMake( 0.0f, 0.0f, bgImage.size.width, bgImage.size.height );
button.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:14.0f];
button.titleLabel.adjustsFontSizeToFitWidth = YES;
button.titleLabel.minimumFontSize = 10.0f;
[button setTitleColor:titleColor forState:UIControlStateNormal];
[button setTitleColor:titleColor forState:UIControlStateHighlighted];
[button setTitle:title forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateHighlighted];
[button setBackgroundImage:bgImage forState:UIControlStateNormal];
[button setBackgroundImage:highlightedBgImage forState:UIControlStateHighlighted];
// You must specify your target object and action selector here.
//
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}