我想和大家分享我的解决方案: 这需要你的子类的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