I’ve an app with a UITableView. We’re regularly changing to SwiftUI, so the UITableView is mainly a group of SwiftUI views. I used to be capable of get the background to look as a gradient utilizing these two features:
func createBackgroundGradient() -> CAGradientLayer {
let gradientLayer = CAGradientLayer()
gradientLayer.colours = [UIColor.red.cgColor, UIColor.white.cgColor, UIColor.red.cgColor]
gradientLayer.areas = [0.0, 0.5, 1.0]
gradientLayer.body = self.tableView.bounds
return gradientLayer
}
func refreshBackground() {
tableView.backgroundView = nil
let backgroundView = UIView()
let gradientView = createBackgroundGradient()
backgroundView.layer.addSublayer(gradientView)
tableView.backgroundView = backgroundView
}
I name this perform from viewWillLayoutSubview as a result of I’ve views popping in as we get information again from the again finish. This works to set the gradient the size of the display. Nonetheless, I need the gradient to take up your complete UITableView, and I need it to scroll with the desk view (which is for much longer than the size of the display), not simply sit within the background with the cells scrolling on prime of it.
The answer to this query: Gradient background coloration with UIScrollView seems just like the conduct I need, however once I change my refreshBackround perform to this:
func refreshBackground() {
if let gradientSublayer = self.gradientSublayer {
gradientSublayer.removeFromSuperlayer()
}
self.gradientSublayer = createBackgroundGradient()
tableView.layer.insertSublayer(self.gradientSublayer ?? createBackgroundGradient(), at: 0)
}
I do not see any distinction in conduct – it is nonetheless simply the size of the display and it would not change place once I scroll.
Does anybody know of a method round this? I’ve considered attempting to paint the completely different sections with completely different parts of the gradient, however that looks like it’s going to get actually difficult as a result of the assorted sections within the desk might or might not be there based mostly on the information we’re getting from the again finish (and it will most likely look janky as a result of parts of the gradient can be popping in as these sections are populated).