是否可以在背景图层下放置CAGradientLayer?

时间:2013-01-12 00:13:03

标签: ios xamarin.ios

我正在为自定义按钮添加渐变图层,以下代码中的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);

这很好用,渐变做到了应有的效果。但是,如果按钮具有背景图像,则不显示背景。渐变似乎位于背景上方的一层上,使其模糊不清。有没有办法解决这个问题,还是我需要在我自己的图层上绘制背景并手动添加它?

2 个答案:

答案 0 :(得分:0)

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

- (void)insertSublayer:(CALayer *)aLayer below:(CALayer *)sublayer

答案 1 :(得分:0)

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

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

因此,这使得UIImageView图层可以保存图像。然后我按照我的问题调用代码,在此ImageView图层下面插入渐变图层 ,然后就可以了。