2015-05-03 9 views
9

Ich habe eine Tableviewcontroller erstellt, wo ich diese Funktion bin Aufruf:Set Steigung hinter UITableView

func setTableViewBackgroundGradient(sender: UITableViewController ,let topColor:UIColor, let 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 = sender.tableView.bounds 
    sender.tableView.backgroundView?.layer.insertSublayer(gradientLayer, atIndex: 0) 

} 

mit:

setTableViewBackgroundGradient(self, UIColor(0xF47434), UIColor(0xEE3965)) 

aber alles, was ich bekommen, ist dies:

enter image description here

Antwort

23

Sie müssen eine Hintergrundansicht erstellen und der Zelle zuweisen:

func setTableViewBackgroundGradient(sender: UITableViewController, _ 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 = sender.tableView.bounds 
    let backgroundView = UIView(frame: sender.tableView.bounds) 
    backgroundView.layer.insertSublayer(gradientLayer, atIndex: 0) 
    sender.tableView.backgroundView = backgroundView 
} 

Vergessen Sie auch nicht UITableViewCell Hintergrundfarbe zu setzen Farbe zu löschen:

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

Yay, Sie meinen Tag gerettet, vielen Dank! –

+0

sehr schöne Lösung, danke –

+0

Zellfarbe kann auch in Storyboard gesetzt werden. –