Set gradient behind UITableView

后端 未结 1 799
北荒
北荒 2020-12-14 11:27

I have created a tableViewController where I\'m calling this function:

func setTableViewBackgroundGradient(sender: UITableViewController ,let topColor:UIColo         


        
相关标签:
1条回答
  • 2020-12-14 11:54

    You need to create background view and assign it to cell:

    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
    }
    

    Also don't forget to set UITableViewCell background color to clear color:

    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        cell.backgroundColor = UIColor.clearColor()
    }
    
    0 讨论(0)
提交回复
热议问题