2016-10-13 95 views
1

我有一個標題爲UITableView,它有一個高度爲10的標題,它的backgroundColorclearColor,只是爲了顯示更分離的部分。現在我只需要在每個部分的底部添加陰影。我已經嘗試向viewForHeaderInSection方法添加陰影,但結果非常糟糕。任何提示實現這一目標?如何僅在UITableView的底部添加陰影?

func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { 
    let shadowView = UIView() 

    shadowView.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5).CGColor 
    shadowView.layer.shadowOffset = CGSizeMake(0.0, 2.0) 
    shadowView.layer.shadowOpacity = 1.0 
    shadowView.layer.shadowRadius = 0.0 
    shadowView.layer.masksToBounds = false 

    return shadowView 
} 

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 
    return 2 
} 
+0

您試過了哪些代碼? – Rob

+0

我編輯了這個問題。 – Slavcho

+0

您可以使用添加圖像視圖(以及陰影圖像)來隱藏離開最後一個單元格的所有單元格的圖像視圖。這將解決您的問題。 –

回答

5

我提出這兩個例子夫特3。首先它是一個經典的影子,我用CAGradientLayer。第二個是陰影弧,我用UIBezierPath,CAShapeLayerCAGradientLayer


經典影:

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { 
    let shadowView = UIView() 




    let gradient = CAGradientLayer() 
    gradient.frame.size = CGSize(width: myTab.bounds.width, height: 15) 
    let stopColor = UIColor.gray.cgColor 

    let startColor = UIColor.white.cgColor 


    gradient.colors = [stopColor,startColor] 


    gradient.locations = [0.0,0.8] 

    shadowView.layer.addSublayer(gradient) 


    return shadowView 
} 

這是結果:

enter image description here

影弧:

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { 
let shadowView = UIView() 

let path = UIBezierPath() 
     path.move(to: CGPoint.zero) 
     path.addLine(to: (CGPoint(x: (myTab.bounds.maxX), y: 0.0))) 
     path.addLine(to: (CGPoint(x: (myTab.bounds.maxX), y: 15))) 
     path.addCurve(to: (CGPoint(x: 0.0, y: 15)), controlPoint1: (CGPoint(x: (myTab.bounds.midX), y: -10)), controlPoint2: (CGPoint(x: 0.0, y: 15))) 
     path.addLine(to: CGPoint.zero) 



     let shape = CAShapeLayer() 
     shape.path = path.cgPath 

     let gradient = CAGradientLayer() 

     gradient.frame.size = CGSize(width: myTab.bounds.width, height: 15) 
     let stopColor = UIColor.gray.cgColor 

     let startColor = UIColor.white.cgColor 


     gradient.colors = [stopColor,startColor] 


     gradient.locations = [0.0,0.9] 

     gradient.mask = shape 

     shadowView.backgroundColor = UIColor.white 
     shadowView.layer.addSublayer(gradient) 




     return shadowView 


} 

這是結果:

enter image description here

+0

這是我需要的。第一種方法很好。如果你在問題中實現了它,它會添加頁腳視圖,然後在它下面會出現陰影。 – Slavcho