Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS7 custom button become transparent when highlighted

I just create my own custom button that seem like iOS6 and earlier buttons but on iOS7 my CustomButton turn transparent when highlighted. I have try to set property adjustsImageWhenHighlighted at NO but nothing change, [self setAlpha:1.f] doesn't work either.

Following, my init method:

-(void) baseInit:(CGRect)frame :(CGFloat *)colorsNormal :(CGFloat *)colorsHighlighted :(CGFloat *)colorsDisabled
{

    self.layer.borderWidth=1.0f;
    self.layer.borderColor=[[UIColor colorWithRed:0.67f green:0.67f blue:0.67f alpha:1.0f] CGColor];
    self.layer.cornerRadius=7.0f;

    self.clipsToBounds = YES;

    UIImage *image = [self generateUIImageWithGradient:frame :colorsNormal];
    [self setBackgroundImage:image forState:UIControlStateNormal];
    [self setTitleColor:[UIColor colorWithRed:0.22f green:0.33f blue:0.53f alpha:1.0f] forState:UIControlStateNormal];

    UIImage *image2 = [self generateUIImageWithGradient:frame :colorsHighlighted];
    [self setBackgroundImage:image2 forState:UIControlStateHighlighted];
    [self setTitleColor:[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:1.0f] forState:UIControlStateHighlighted];

    UIImage *image3 = [self generateUIImageWithGradient:frame :colorsDisabled];
    [self setBackgroundImage:image3 forState:UIControlStateDisabled];
}

-(id) generateUIImageWithGradient:(CGRect)frame :(CGFloat *)colors
{
    CGSize size = CGSizeMake(frame.size.width, frame.size.height);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();

    size_t gradientNumberOfLocations = 2;
    CGFloat gradientLocations[2] = {0.0, 1.0};

    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, colors, gradientLocations, gradientNumberOfLocations);

    CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(0, size.height), 0);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorspace);
    UIGraphicsEndImageContext();

    return image;
}

And my method call :

CGFloat colorsNormal [] = {1.0f,1.0f,1.0f,1.0f,1.0f,1.0f,1.0f,1.0f};
CGFloat colorsHighlighted [] = {0.03f,0.55f,0.97f,1.0f,0.0f,0.37f,0.90f,1.0f};
CGFloat colorsDisabled [] = {1.0f,1.0f,1.0f,1.0f,1.0f,1.0f,1.0f,1.0f};

[self baseInit:rect :colorsNormal :colorsHighlighted :colorsDisabled];

That work like a charm on iOS6 and NormalState work on iOS7.

Thanks for your help

like image 344
Alex R. Avatar asked Oct 09 '13 09:10

Alex R.


1 Answers

Set the button type property to UIButtonTypeCustom seem to work. Problem solved.

like image 79
Alex R. Avatar answered Nov 01 '22 09:11

Alex R.