2017-12-31 25 views
1

나는 꽤 많은 시간 동안 그라데이션 레이어를 tableView에 추가하는 방법을 알아 내려고 노력했지만 많은 다른 것들을 시도했지만 결코 테이블 뷰 셀을 표시 할 수는 없지만, 아무리 많은 Subviews 나 sendSubviews를 가져도 상관 없습니다.그라디언트 레이어를 테이블 뷰에 추가하기

let gradientLayer = CAGradientLayer() 

gradientLayer.frame = view.bounds 

gradientLayer.colors = [UIColor(red: 125/255.0, green: 125/255.0, blue: 125/255.0, alpha: 1.0).cgColor, UIColor(red: 125/255.0, green: 125/255.0, blue: 255/255.0, alpha: 1.0).cgColor] 

gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0) 
gradientLayer.endPoint = CGPoint(x: 0.0, y: 1.0) 

tableView.layer.addSublayer(gradientLayer) 

는 상관없이 파단 완료 보낼 얼마나 많은 파단을 가져다의 조합, 난 항상이를 얻을 수가 없으며, 세포는 앞으로 나오게하지 않습니다

enter image description here

사람이 알고 있나요 이 문제를 해결하는 방법 또는 어떻게 작동하게하려면 다른 방법을하고 싶습니까?

도움을 주신 데 대해 감사드립니다.

답변

2

배경보기를 만들고 TableView에 할당 :

func setGradientToTableView(tableView: UITableView, _ topColor:UIColor, _ bottomColor:UIColor) { 

    let gradientBackgroundColors = [topColor.CGColor, bottomColor.CGColor] 
    let gradientLocations = [0.0,1.0] 

    let gradientLayer = CAGradientLayer() 
    gradientLayer.colors = gradientBackgroundColors 
    gradientLayer.locations = gradientLocations 

    gradientLayer.frame = tableView.bounds 
    let backgroundView = UIView(frame: tableView.bounds) 
    backgroundView.layer.insertSublayer(gradientLayer, atIndex: 0) 
    tableView.backgroundView = backgroundView 
} 

또한 TableView 셀의 투명 색상 :

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    cell.backgroundColor = UIColor.clearColor() 
} 
+0

내가있는 tableview 컨트롤러를 사용하고 있지 않다. 일반 뷰 컨트롤러에서 어떻게 처리 할 것입니까? –

+0

원래 있던 곳에 모든 코드를 넣고 tableView 앞에서'sender.'를 제거하십시오. –

+0

Swift 4에서는'backgroundView.layer.insertSublayer (gradientLayer, at : 0)'이어야합니다. 또한 tableView의 레이아웃이 변경되면 (회전 등) 그라디언트 레이어가 뒤 따르지 않는다는 사실을 잊지 마십시오. 'viewDidLayoutSubviews'를 오버라이드 (override) 해 거기의 레이어를 갱신 할 필요가 있습니다. 그렇지 않으면 멋진 솔루션 그래서 upvote 있습니다. –

관련 문제