【问题标题】:UITableView Shadows top and bottomUITableView 阴影顶部和底部
【发布时间】:2011-06-09 21:23:06
【问题描述】:

在我的一个视图中,我使用的是 UIWebView,我喜欢背景视图顶部和底部以及滚动区域顶部和底部的默认阴影效果。

时钟应用程序中也有相同的,具有自定义背景(蓝线与世界地图),所以我猜也可以使用 UITableView...

这是我的网络视图,我想添加到表格视图中。我的网络视图:

所以我在这里添加了一个自定义背景(浅灰色纹理)。这是我添加相同背景的另一个视图的下方,但是您可以看到没有阴影...我猜这不是 UIScrollView 的内置效果。有没有办法对表格视图做同样的事情?

我的表格视图:

更新 1:

我找到了这篇很棒的文章UITableView Shadows,但视图底部没有阴影。

【问题讨论】:

    标签: iphone uitableview uiscrollview gradient


    【解决方案1】:

    视图的一个基本部分是它的层,它是一个 CALayer。您可以通过视图的 layer 属性访问它:

    myView.layer
    

    在文档中,CALayers 有几个控制阴影的属性。您应该能够告诉导航栏的图层投射阴影。连同您想要投射阴影的任何其他内容的图层。

    myView.layer.shadowOffset
    myView.layer.shadowRadius
    

    您需要确保将 QuartzCore 框架添加到您的项目中才能访问此属性。

    【讨论】:

      【解决方案2】:

      这是我在 Swift 中的代码,我认为它运行良好:

      func shadowView (view : UIView , r : CGFloat, gr : CGFloat , b : CGFloat , header : Bool)
          {
              let bottomColor = UIColor (red: r/255 , green: gr/255 , blue: b/255 , alpha: 0)
      
              //var bottomColor = UIColor ( red: (r/255), green : (g/255), blue : (b/255), alpha : 0)
              //var topColor = UIColor ( red: (r/255), green : (g/255), blue : (b/255), alpha : 1)
              let topColor = UIColor (red: r/255, green: gr/255, blue: b/255, alpha: 1)
              var arrayColors: [CGColor] = [
                  topColor.CGColor,
                  bottomColor.CGColor
              ]
      
              if header
              {
                  let headerShadow: CAGradientLayer = CAGradientLayer()
      
                  headerShadow.frame = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: view.frame.size.height)
                  headerShadow.colors = arrayColors
                  view.layer.insertSublayer(headerShadow, atIndex: 1)
              } else
              {
                  let footerShadow: CAGradientLayer = CAGradientLayer()
      
                  arrayColors  = [
                      bottomColor.CGColor,
                      topColor.CGColor
                  ]
                  footerShadow.frame = CGRect(x: 0, y: 0 , width: UIScreen.mainScreen().bounds.width, height: view.frame.size.height)
                  footerShadow.colors = arrayColors
                  view.layer.insertSublayer(footerShadow, atIndex: 1)
              }
          }
      

      【讨论】:

        【解决方案3】:

        这适用于UITableViewControllerUIViewController。 在我的viewDidLoad: 中,我使用以下代码:

        [self.view addSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shadow.png"]]];
        

        这将在导航栏下方添加阴影。

        这是我在 Photoshop 中拼凑的影子:

        shadow@2x.png:

        【讨论】:

        • 我得到了这个,我只需要在我的视图底部有一个阴影。这个视图是一个导航控制器,但有时会在我的主视图中与我的标签栏一起加载,也可以作为没有标签栏的模态视图......
        【解决方案4】:

        我的代码。例如。对于页脚中的阴影:

        - (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
        {
            UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,tableView.bounds.size.width, 11)];
        
            UIImageView *shadow = [[UIImageView alloc] initWithImage:[UIImage 
                                                          imageNamed:@"tableHeaderBottom"]];
            [shadow sizeToFit];
            [footerView addSubview:shadow];
        
            return footerView;
        }
        
        - (void)viewDidLoad
        {
            [self.tableView setContentInset:UIEdgeInsetsMake(0, 0, -11, 0)];
            [super viewDidLoad];
        }
        

        图片:

        对于标题:使用相同的代码,但只是垂直翻转图像并在第一部分 (0) 上使用viewForHeaderInSection

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-10-04
          • 1970-01-01
          • 2018-12-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-25
          • 1970-01-01
          相关资源
          最近更新 更多