Thursday, May 30, 2013

UIButton setBackgroundImage forState example in Objective C (iOS).

UIButton setBackgroundImage forState

Sets the background image to use for the specified button state.

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

Parameters of [UIButton setBackgroundImage forState]
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 of [UIButton setBackgroundImage forState]
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.

UIButton setBackgroundImage forState example.
UIButton *btnClear = [[UIButton alloc] init];
btnClear = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

btnClear.frame = CGRectMake(115, 200, 90, 40);

[btnClear setTitle:@"Clear" forState:UIControlStateNormal];

[btnClear setBackgroundImage:[UIImage imageNamed:@"blue_button.png"]
                            forState:UIControlStateNormal];

[btnClear addTarget:self action:@selector(clearAction:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btnClear];

Example of [UIButton setBackgroundImage forState].
+ (UIButton *)buttonWithTitle:(NSString *)title
                       target:(id)target
                     selector:(SEL)selector
                        frame:(CGRect)frame
                        image:(UIImage *)image
                 imagePressed:(UIImage *)imagePressed
                darkTextColor:(BOOL)darkTextColor
    {
   UIButton *button = [UIButton alloc] initWithFrame:frame;

   button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
   button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

   [button setTitle:title forState:UIControlStateNormal];
   [button setTitleColor:UIColor blackColor forState:UIControlStateNormal];

   UIImage *newImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
   [button setBackgroundImage:newImage forState:UIControlStateNormal];

   UIImage *newPressedImage = [imagePressed stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
   [button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];

   [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];

   // in case the parent view draws with a custom color or gradient, use a transparent color
   button.backgroundColor = UIColor clearColor;

   return button;
}

UIButton setBackgroundImage forState example.
+ (id) customButtonWithImageNamed:(NSString *)imageName selectedImageNamed:(NSString *)selectedImageName leftCapWidth:(CGFloat)leftCapWidth edgeInsets:(UIEdgeInsets)edgeInsets title:(NSString *)title target:(id)target selector:(SEL)selector {

    UIButton* customButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [customButton addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    customButton.titleLabel.font = [UIFont boldSystemFontOfSize:12.0f];
    customButton.titleLabel.shadowColor = [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:0.25f];
    customButton.titleLabel.shadowOffset = CGSizeMake(0.0f, -1.0f);
    customButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
    customButton.titleEdgeInsets = edgeInsets;
    UIImage* navButtonBackgroundImage = [[UIImage imageNamed:imageName] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0.0f];
    UIImage* navButtonPressedBackgroundImage = [[UIImage imageNamed:selectedImageName] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0.0f];
    [customButton setBackgroundImage:navButtonBackgroundImage forState:UIControlStateNormal];
    [customButton setTitle:title forState:UIControlStateNormal];
    [customButton setBackgroundImage:navButtonPressedBackgroundImage forState:UIControlStateHighlighted];
    [customButton setBackgroundImage:navButtonPressedBackgroundImage forState:UIControlStateSelected];

    CGSize size = CGSizeMake(30.0f, 30.0f);
    if (title != nil) {
        size = [[NSString stringWithString:title] sizeWithFont:customButton.titleLabel.font];
    }
    customButton.frame = CGRectMake(0.0f, 0.0f, size.width + 20.0f, 30.0f);
    customButton.layer.shouldRasterize = YES;
    customButton.layer.rasterizationScale = [[UIScreen mainScreen] scale];
    return [[[UIBarButtonItem alloc] initWithCustomView:customButton] autorelease];
}

End of UIButton setBackgroundImage forState example article.