我發現我的問題的解決方案,它更像是一種解決方法,但對我來說工作正常,我創建了自定義類ShadowVIew,它爲任何UIView進行適當的設置,並在其下方添加一個黑色透明視圖看起來像一個影子:
下面的代碼:
ShadowView.m
#import "ShadowView.h"
@interface ShadowView()
@property (nonatomic) UIView * shadow;
@end
@implementation ShadowView
-(id)initWithCoder:(NSCoder *)aDecoder{
if ((self = [super initWithCoder:aDecoder])) {
self.layer.masksToBounds = NO;
self.layer.cornerRadius = 8; // if you like rounded corners
}
return self;
}
-(void)layoutSubviews{
[super layoutSubviews];
if (self.shadow.superview == nil) {
UIView * parent = self.superview;
self.shadow = [[UIView alloc] initWithFrame:self.frame];
self.shadow.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
CGRect frame = self.shadow.frame;
frame.origin.y+=1;
frame.origin.x-=1;
frame.size.width+=2;
self.shadow.frame = frame;
self.shadow.layer.masksToBounds = NO;
self.shadow.layer.cornerRadius = 8;
[parent insertSubview:self.shadow atIndex:0];
}
@end
記住,這不是完美的解決方案,但對於一個快速實現正常工作。如果你的視圖沒有經常加載,你可以簡單地添加由石英繪製的陰影,所以它不會影響應用程序的性能。
self.layer.shadowOffset = CGSizeMake(0, 1);
self.layer.shadowRadius = 1;
self.layer.shadowOpacity = 0.3;
希望它可以幫助別人:)