let colors = ["#97E9A0".hexToColor().withAlphaComponent(0.43).cgColor,"#FFFFFF".hexToColor().withAlphaComponent(1).cgColor]
let gradientLayer = CAGradientLayer()
gradientLayer.frame = tableView.bounds
let colors = [UIColor.red.cgColor,UIColor.blue.cgColor]
gradientLayer.colors = colors
gradientLayer.startPoint = CGPoint(x: 0.5, y: 0)
gradientLayer.endPoint = CGPoint(x: 0.5, y: 1)
gradientLayer.cornerRadius = 6
gradientLayer.zPosition = -1
gradientLayer.masksToBounds = true
tableView.layer.insertSublayer(gradientLayer, at: 0)
重点在于
gradientLayer.zPosition = -1 //确保图层在视图的最底层
tableView.layer.insertSublayer(gradientLayer, at: 0) //把layer放到最底层