Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Round Rect Button background color on StateHighlighted

I'm trying to change the background color of a button when it's selected and don't want to use an image.

[mBtn setBackgroundColor:[UIColor grayColor] forState:UIControlStateHighlighted];

Any thoughts?

like image 238
Syntax Avatar asked Aug 30 '11 03:08

Syntax


1 Answers

I'm replying to this old thread because it pops up consistently in searches for a solution to this problem and I have seen no solution elsewhere. It is truly annoying that setTintColor only applies to the highlighted state of a UIButton. Six months ago, it was equally annoying that it applied only to iOS 5, but that will hopefully be less of an issue going forward. With that in mind, I've drawn upon and combined a number of community suggestions to composite a general purpose solution to tinting a group of buttons in their normal state.

The method below accepts an NSArray of UIButtons and a set of color specifications as input. It applies the color specifications to one button using setTintColor, renders the result to a UIImage, and applies that image as the background image of the entire set of buttons. This avoids the need to create discrete image files for button colors. Also, it does so using a stretchable image so that it may work with a collection of buttons of different sizes (though note that it assumes the default corner rounding factors of UIButton). I hope you'll find it useful for iOS 5 targets.

- (void) setColorOfButtons:(NSArray*)buttons red:(float)red green:(float)green blue:(float)blue alpha:(float)alpha {

    if (buttons.count == 0) {
        return;
    }

    // get the first button
    NSEnumerator* buttonEnum = [buttons objectEnumerator];
    UIButton* button = (UIButton*)[buttonEnum nextObject];

    // set the button's highlight color
    [button setTintColor:[UIColor colorWithRed:red/255.9999f green:green/255.9999f blue:blue/255.9999f alpha:alpha]];

    // clear any existing background image
    [button setBackgroundImage:nil forState:UIControlStateNormal];

    // place the button into highlighted state with no title
    BOOL wasHighlighted = button.highlighted;
    NSString* savedTitle = [button titleForState:UIControlStateNormal];
    [button setTitle:nil forState:UIControlStateNormal];
    [button setHighlighted:YES];

    // render the highlighted state of the button into an image
    UIGraphicsBeginImageContext(button.layer.frame.size);
    CGContextRef graphicsContext = UIGraphicsGetCurrentContext();
    [button.layer renderInContext:graphicsContext];
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
    UIImage* stretchableImage = [image stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    UIGraphicsEndImageContext();

    // restore the button's state and title
    [button setHighlighted:wasHighlighted];
    [button setTitle:savedTitle forState:UIControlStateNormal];

    // set background image of all buttons
    do {
        [button setBackgroundImage:stretchableImage forState:UIControlStateNormal];
    } while (button = (UIButton*)[buttonEnum nextObject]);    
}
like image 82
Scott DellaPeruta Avatar answered Oct 17 '22 06:10

Scott DellaPeruta