我想和大家分享我的解決方案: 這需要你的子類的UITableView並添加屬性,示範的緣故,讓我們showShadow調用它。添加到您的表視圖的.h文件中:
@property (nonatomic,assign) BOOL showShadow;
,並在.m文件及其相應的@synthesize創建getter和setter方法:
@synthesize showShadow;
然後加入伊娃UIView *shadowView;
到表視圖的.h文件。 現在的- (id)initWithFrame:(CGRect)frame
方法的子類的UITableView添加下面的代碼來設置,最終將投下的陰影角度:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
shadowView = [[UIView alloc]initWithFrame:self.frame];
shadowView.backgroundColor = [UIColor whiteColor];
shadowView.layer.shadowOpacity = 0.1;
shadowView.layer.shadowOffset = CGSizeMake(3, 3);
shadowView.layer.shadowRadius = 1;
}
return self;
}
最後,寫的setter方法來顯示/隱藏陰影:
-(void)setShowShadow:(BOOL)s{
showShadow = s;
if(s){
[self.superview insertSubview:shadowView belowSubview:self];
}else{
[shadowView removeFromSuperview];
}
}
此外,如果你想移動的表(無論何種原因),你應該重寫-setFrame:
方法也與它一起移動shadowView(因爲它不是在表視圖的視圖層次):
-(void)setFrame:(CGRect)frame{
[super setFrame:frame];
shadowView.frame = frame;
}
您已成功啓用陰影!使用這樣的:
MySubclassedTableView *table = [[MySubclassedTableView alloc]initWithFrame:CGRectMake(20, 200, 280, 200)];
[self.view addSubview:table];
table.showShadow = YES;
警告:
你必須設置showShadow屬性後你把你的表視圖,因爲該行table.showShadow將調用行[個體經營.superview insertSubview:shadowView belowSubview:self];這需要表視圖存在。
你確定'clipToBounds'和'maskToBounds'設置爲'NO'(分別爲視圖和圖層)? – mattjgalloway
謝謝你,你是對的!如果您添加答案,我會將其標記爲正確答案。 –
已回答。很高興它解決了它! – mattjgalloway