2011-06-09 134 views
6

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

有一个在时钟应用程式一样,用一个自定义背景(与世界地图蓝线),所以它使用一个UITableView也是我猜的可能性...

这是我和我的Web视图,并且我想添加到表格视图。我的Web视图:

enter image description here

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

我的表视图:

enter image description here

更新1:

我发现这篇大文章UITableView Shadows但没有影子的视图的底部。

回答

2

这适用于UITableViewControllerUIViewController。 在我viewDidLoad:我使用下面的代码:

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

这将增加不到导航栏中的阴影。

这是我的影子,我在Photoshop扔在一起:

enter image description here

+0

我得到了这一个,我只需要在底部我的看法了一层阴影。而这个视图是一个导航控制器,但有时加载在我的主视图与我的标签栏,也作为模式视图没有标签栏... – Dachmt 2011-06-10 15:36:48

3

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

myView.layer 

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

myView.layer.shadowOffset 
myView.layer.shadowRadius 

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

0

我的代码。例如。为影子在页脚:

- (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]; 
} 

这些图像:
Normal image Retina image

有关header:使用相同的代码,但只是翻转图像垂直和在所述第一部分(0)的使用viewForHeaderInSection

8

这是我在斯威夫特的代码,我觉得它工作得很好:

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) 
     } 
    }