Reputation: 4502
I have a UITableViewController I am trying to apply a gradient background on.
I would like the gradient to be on the entire view and not just the a cell.
I have an extension with a function I am calling in viewDidLoad
. I am expecting this to be applied and visible behind my UITableView
However the background is always white. I set view.backgroundColor = UIColor.clear
incase this was just a white layer over my gradient, however it does not appear so as the background is now just black.
HomeController.swift
extension HomeController {
fileprivate func applyBackgroundGradientView() -> Void {
let frame = CGRect(x: 0.0, y: 0.0, width: view.frame.size.width, height: view.frame.size.height)
let bgView = UIView(frame: frame)
bgView.layer.insertSublayer(gradientBackgroundView, at: 0)
tableView.backgroundView = bgView
}
}
GradientBackgroundView.swift
class GradientBackgroundView: CAGradientLayer {
override init() {
super.init()
setupGradientLayer()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
fileprivate func setupGradientLayer() -> Void {
self.colors = ["226320","256c23","39a636","267024"].map({ hex -> CGColor in
return UIColor.hexStringToUIColor(hex: hex).cgColor
})
self.calculatePoints(for: 45)
}
}
Upvotes: 0
Views: 39
Reputation: 2478
I don't see a frame on your CAGradientLayer
.
If you do not setup the frame, your view will not show.
let gradientBackgroundView = GradientBackgroundView()
gradientBackgroundView.frame = CGRect(x: 0.0, y: 0.0, width: view.frame.size.width, height: view.frame.size.height)
view.layer.insertSublayer(gradientBackgroundView, at: 0)
Upvotes: 1