2011-06-09 131 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) 
     } 
    }