esreli
esreli

Reputation: 5061

Place CALayer at very bottom of layer stack of UIButton

This seems simple enough - perhaps you can help me find the solution?

I wrote a custom drop shadow for a UIButton. I'm attempting to place it at the lowest index of the UIButton's layer stack. Unfortunately, it isn't being placed beneath the rest of the UIButton.

The Code:

//UIButton type custom
alarmSetterButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img = [UIImage imageNamed:@"rootSchedulerButton"];

//Scaling
float delta = 2.4f;
if (IS_IPHONE_5) delta = 2.0f;

[alarmSetterButton setFrame:CGRectMake(0.0f, 0.0f, (img.size.width/delta), (img.size.height/delta))];
[alarmSetterButton setBackgroundImage:img forState:UIControlStateNormal];
[alarmSetterButton setTitle:@"" forState:UIControlStateNormal];
[alarmSetterButton addTarget:self action:@selector(setSecondaryView:) forControlEvents:UIControlEventTouchUpInside];

//the colors for the gradient
UIColor *high = [UIColor colorWithWhite:0.0f alpha:1.0f];
UIColor *low = [UIColor colorWithWhite:0.0f alpha:0.0f];

//The gradient, simply enough.  It is a rectangle
CAGradientLayer * gradient = [CAGradientLayer layer];
[gradient setFrame:[b bounds]];
[gradient setColors:[NSArray arrayWithObjects:(id)[high CGColor], (id)[low CGColor], nil]];
[gradient setAnchorPoint:CGPointMake(0.5f, 0.0f)];
[gradient setTransform:CATransform3DMakeRotation(315.0 / 180.0 * M_PI, 0.0, 0.0, 1.0)];

// What needs altered
[alarmSetterButton.layer insertSublayer:gradient below:alarmSetterButton.layer];

Upvotes: 0

Views: 845

Answers (2)

esreli
esreli

Reputation: 5061

I found the solution. Instead of using UIButton's setBackroundImage:forState: method I wrote a new CALayer to contain the contents of the image.

Thanks for the help.

:)

Upvotes: 1

Milo
Milo

Reputation: 5091

Try this, it places the layer at position zero in the stack, i.e. the bottom.

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

Upvotes: 0

Related Questions