2017-04-26 76 views
0

我試圖在我的iOS應用程序的表格視圖中放置陰影。我嘗試了不同的技術,但還沒有設法解決它。我試圖增加該小區本身的第一層上的陰影:在UITableView單元格上放置陰影不顯示[Swift 3]

cell.layer.shadowColor = UIColor.black.cgColor 
cell.layer.shadowOpacity = 1 
cell.layer.shadowOffset = CGSize.zero 
cell.layer.shadowRadius = 2 

這並沒有工作,所以我試圖把盒子上影內容視圖:

cell.contentview.layer.shadowColor = UIColor.black.cgColor 
cell.contentview.layer.shadowOpacity = 1 
cell.contentview.layer.shadowOffset = CGSize.zero 
cell.contentview.layer.shadowRadius = 2 

我還嘗試在表視圖單元格中添加具有相同約束的額外視圖,並在該視圖上放置陰影框,但這並未改變結果。 result with extra view and constraints and cornenradius

東西在表視圖特別是以下內容:每節

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    if section != 0 { 
     return 10 
    } else { 
     return 0 
    } 
} 

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 
    let header = view as! UITableViewHeaderFooterView 
    header.tintColor = UIColor.init(netHex: 0xF9FAFD) 
} 

表視圖的數據都放在單獨的部分,和1排。

+0

看到這個曾經http://stackoverflow.com/questions/43567288/uiview-shadow -cornerradius-not-working/43567400#43567400 –

+0

我嘗試過那裏解釋過的解決方案,但它沒有效果。 –

+0

可以更新問題一次 –

回答

2

據我,你應該做以下:

1)包裝在containerView所有的UITableViewCell意見。

2)爲每個邊上的父容器視圖賦予10px的邊距。

3)然後申請

cell.containerView.layer.shadowColor = UIColor.black.cgColor 
cell.containerView.layer.shadowOpacity = 1 
cell.containerView.layer.shadowOffset = //any offset 
cell.containerView.layer.shadowRadius = 2 

注:柵格化細胞層的內容,以避免總是重繪

cell.layer.shouldRasterize = true 
+0

我如何將邊距添加到容器視圖?我現在有這個代碼:cell.backView.layer.shadowColor = UIColor.black.cgColor cell.backView.layer.shadowOpacity = 1.0 cell.backView.layer.shadowOffset = CGSize.zero cell.backView.layer。 shadowRadius = 2.0 cell.backView.layer.cornerRadius = 15結果如下:![結果](http://nl.tinypic.com/r/6ekfaw/9)。 –

+0

使用像領先,traling,頂部和底部約束自動佈局。到10 – ankit

+0

這是我嘗試這樣的結果:[帶邊緣的結果](http://nl.tinypic.com/r/xm9a8j/9)和[結果模擬器](http://nl.tinypic.com/r/32zl1et/9) –