1

我正在向自定义按钮添加渐变层,this以下代码中的引用:

//a custom gradient
var layerGradient = new CAGradientLayer();

//the gradient colors are the base color to the modified version
layerGradient.Colors = new CGColor[]{color.CGColor, color2.CGColor};

//add the gradient as a sublayer in the button
this.Layer.InsertSublayer(layerGradient, 0);

这工作正常,渐变做它应该做的。但是,如果按钮有背景图像,则不会显示背景。渐变似乎位于背景上方的一层上,将其遮蔽。有什么办法可以解决这个问题,还是我需要在自己的图层上绘制背景并手动添加?

4

2 回答 2

0

我通过将背景图像添加为自己的图层来解决此问题,然后在其下方添加渐变图层:

mImageView = new UIImageView();
this.Layer.InsertSublayer(mImageView.Layer, 0);
mImageView.Image = mImage;

所以这使得 UIImageView 层来保存图像。然后我像我的问题一样调用代码,在这个 ImageView 层下面插入渐变层,它就可以工作了。

于 2013-01-12T23:12:33.483 回答
0

没有尝试过,但可能insertSublayer:below:是您正在寻找的。

- (void)insertSublayer:(CALayer *)aLayer below:(CALayer *)sublayer
于 2013-01-12T01:36:57.717 回答