iphone - CALayer 边框出现在 subview 上方(我认为与 Z 顺序相关)

标签 iphone calayer z-order

我已搜索但找不到此行为的原因。

我有一个正在设置其图像的 UIButton。以下是该按钮的显示方式。请注意,这只是预期按钮设计的 Photoshop:

enter image description here

本质上,它是一个方形的自定义 UIButton,带有白色边框和一点周围的阴影。右上角有一个“X”标记,它将以编程方式添加为 subview 。

这是实际应用程序中按钮的屏幕截图。此时,我仅添加了阴影和 X 标记作为 subview :

enter image description here

如何,当我尝试添加白色边框时,它看起来像这样:

enter image description here

白色边框似乎出现在 X 标记子图层上方。我不知道为什么。

这是我正在使用的代码:

// selectedPhotoButton is the UIButton with UIImage set earlier
// At this point, I am adding in the shadow
[selectedPhotoButton layer] setShadowColor:[[UIColor lightGrayColor] CGColor]];
[[selectedPhotoButton layer] setShadowOffset: CGSizeMake(1.0f, 1.0f)];
[[selectedPhotoButton layer] setShadowRadius:0.5f];
[[selectedPhotoButton layer] setShadowOpacity:1.0f]; 

// Now add the white border    
[[selectedPhotoButton layer] setBorderColor:[[UIColor whiteColor] CGColor]];
[[selectedPhotoButton layer] setBorderWidth:2.0];

// Now add the X mark subview
UIImage *deleteImage = [UIImage imageNamed:@"nocheck_photo.png"];
UIImageView *deleteMark = [[UIImageView alloc] initWithFrame:CGRectMake(53, -5, 27, 27)];
deleteMark.contentMode = UIViewContentModeScaleAspectFit;
[deleteMark setImage:deleteImage];
[selectedPhotoButton addSubview:deleteMark];
[deleteMark release];

我不明白为什么边框出现在deleteMark subview 上方。有什么办法可以达到预期的效果吗?

谢谢!

最佳答案

摘自 CALayer 上的 Apple 文档:

The border is drawn inset from the receiver’s bounds by borderWidth. It is composited above the receiver’s contents and sublayers and includes the effects of the cornerRadius property.

为了获得您想要的效果,我建议您将图像放入自己的 subview /子图层中,并设置该子图层的 borderWidth 属性。

关于iphone - CALayer 边框出现在 subview 上方(我认为与 Z 顺序相关),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9901603/

相关文章:

ios - 所有 UIView 过渡动画停止工作

ios - 圆形边框不能完美剪裁/遮蔽

ios - 如何在 iOS 中实现透视文字效果?

android - Camera2预览是黑色的,如果我在前面加一个GLSurfaceView

javascript - 如何将点击事件映射到多层中的元素?

javascript - 当选择器元素被另一个元素遮挡时使用 jQuery .on()

ios - 裁剪 UIImageView 和手势识别器

iphone - 如何在标签栏上绘制符号?

iphone - youTube iFrame 嵌入代码是否适用于移动设备? (iPhone, 安卓)

ios - 应用安装失败 : Unknown Error Xcode 7