Dailycode.info

Short solution for short problems

UIButton with gradient background and image on it!

I got a request for an app with lots of gradient styles on all kinds of controls.

When I applied the typical code to the UIButton, the text stayed on but the image disappeared.

So I found that the solution is to bring the UIImageview to the front like this:

[vw bringSubviewToFront:vw.imageView];

Now the complete code to make the UIButton look like this:

is this:

+ (void) MakeNormalButton:(UIButton *) vw

{

    CAGradientLayer *gradient = [CAGradientLayerlayer];

    gradient.frame = vw.bounds;

    gradient.colors = [NSArrayarrayWithObjects:(id)[[UIColorwhiteColor] CGColor], (id)[[UIColorlightGrayColor] CGColor], nil];

    [vw.layer insertSublayer:gradient atIndex:0];

    vw.layer.masksToBounds = NO;

    vw.layer.shouldRasterize = YES;

    [vw.layersetShadowColor:[UIColorblackColor].CGColor];

    [vw.layersetShadowOpacity:0.5];

    [vw.layersetShadowRadius:2.0];

    [vw.layersetShadowOffset:CGSizeMake(2.0f, 2.0f)];

    [vw.layersetCornerRadius:5.0f];

    [vw bringSubviewToFront:vw.imageView];

 

}