【问题标题】:Adding drop shadow to UITableView向 UITableView 添加阴影
【发布时间】:2012-02-19 04:32:50
【问题描述】:

我有一个普通的 UITableView(未分组),我想在左侧和右侧添加阴影。

我怎样才能做到这一点?我试过了:

[self.tableView.layer setShadowColor:[[UIColor whiteColor] CGColor]];
[self.tableView.layer setShadowOffset:CGSizeMake(0, 0)];
[self.tableView.layer setShadowRadius:5.0];
[self.tableView.layer setShadowOpacity:1];

但它不起作用。

【问题讨论】:

  • 您是否确定clipToBounds 和maskToBounds 设置为NO(分别用于视图和图层)?
  • 谢谢你,你是对的!如果您添加答案,我会将其标记为正确答案。
  • 已标记正确答案。再次感谢你。再见:)

标签: iphone objective-c uitableview shadow


【解决方案1】:

添加@mattjgalloway 提供的 Swift 版本的解决方案(对我帮助很大)

your_TableView.clipsToBounds = false
your_TableView..layer.masksToBounds = false
your_TableView..layer.shadowColor = UIColor.lightGray.cgColor
your_TableView..layer.shadowOffset = CGSize(width: 0, height: 0)
your_TableView..layer.shadowRadius = 5.0
your_TableView..layer.shadowOpacity = 0.5

【讨论】:

    【解决方案2】:

    我想分享我的解决方案: 这需要您继承 UITableView 并添加一个属性,为了演示,我们将其称为 showShadow。将此添加到表视图的 .h 文件中:

    @property (nonatomic,assign) BOOL showShadow;

    及其对应的.m文件中的@synthesize来创建getter和setter方法:

    @synthesize showShadow;

    然后将 iVar UIView *shadowView; 添加到表视图的 .h 文件中。 现在在子类 UITableView 的 - (id)initWithFrame:(CGRect)frame 方法中添加以下代码来设置最终将投射阴影的视图:

    - (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 属性 AFTER 添加表格视图,因为行 table.showShadow 将调用行 [self.superview insertSubview:shadowView belowSubview:self];这需要表格视图存在。

    【讨论】:

      【解决方案3】:

      您需要确保在视图和图层上分别将clipsToBounds 和masksToBounds 设置为NO。

      self.tableView.clipsToBounds = NO;
      self.tableView.layer.masksToBounds = NO;
      

      【讨论】:

      • 感谢您的信息。但是如果我设置 tableView.layer.masksToBounds = NO; & tableView.clipsToBounds = 否; ,我的 tableviewcell 移动到 tableviews 边界之上。我的 viewController 中有 tableview , imageview 。因此,表格视图使用了某些部分,而iamgeview 使用了某些部分。你能帮帮我吗?
      • @iOSAppDev 请看看我的回答。它将防止您遇到的问题。
      • 你最好在 UITableView 后面放一个 UIView 并给 UIView 添加阴影。
      • 当我将动态大小的 UItableVIew 的 cliptoBounds 和 masktoBounds 设置为 false 时,我遇到了一个奇怪的问题。这是同一 drive.google.com/file/d/1_BGThAIbJgY8blxLLFDKD8_H76Vdx7Q5/… 的重新编码视频
      【解决方案4】:

      我看到的不是白光的影子吗?您没有设置偏移量,因此它完全按照您的意愿行事。对于阴影,将颜色设置为黑色,并将其偏移量设置为 3.5 或其他值。

      【讨论】:

      • 不,该图像是我试图实现的。现在我已经修复了 mattjgalloway 评论。无论如何,谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-06
      • 1970-01-01
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-03
      相关资源
      最近更新 更多