将CALayer放置在UIButton的图层堆栈的最底部

时间:2014-08-04 21:41:53

标签: ios objective-c uibutton calayer

这看起来很简单 - 也许你可以帮我找到解决方案?

我为UIButton写了一个自定义投影。我试图将它放在UIButton层堆栈的最低索引处。不幸的是,它没有放在UIButton的其余部分之下。

守则:

//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];

2 个答案:

答案 0 :(得分:1)

我找到了解决方案。我没有使用UIButton的setBackroundImage:forState:方法,而是编写了一个新的CALayer来包含图像的内容。

感谢您的帮助。

:)

答案 1 :(得分:0)

试试这个,它将图层置于堆栈中的零位置,即底部。

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