我已搜索但找不到此行为的原因。
我有一个正在设置其图像的 UIButton。以下是该按钮的显示方式。请注意,这只是预期按钮设计的 Photoshop:
本质上,它是一个方形的自定义 UIButton,带有白色边框和一点周围的阴影。右上角有一个“X”标记,它将以编程方式添加为 subview 。
这是实际应用程序中按钮的屏幕截图。此时,我仅添加了阴影和 X 标记作为 subview :
如何,当我尝试添加白色边框时,它看起来像这样:
白色边框似乎出现在 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/