I’ve an app with a UITableView. We’re step by step changing to SwiftUI, so the UITableView is principally a group of SwiftUI views. I used to be in a position to 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.places = [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 operate from viewWillLayoutSubview as a result of I’ve views popping in as we get knowledge again from the again finish. This works to set the gradient the size of the display screen. Nonetheless, I would like the gradient to take up the whole UITableView, and I would like it to scroll with the desk view (which is for much longer than the size of the display screen), not simply sit within the background with the cells scrolling on prime of it.
The answer to this query: Gradient background shade with UIScrollView seems to be just like the habits I would like, however after I change my refreshBackround operate 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 habits – it is nonetheless simply the size of the display screen and it does not change place after I scroll.
Does anybody know of a manner round this? I’ve thought of making an attempt to paint the completely different sections with completely different parts of the gradient, however that looks as if it’s going to get actually difficult as a result of the varied sections within the desk might or will not be there primarily based on the information we’re getting from the again finish (and it will in all probability look janky as a result of parts of the gradient will probably be popping in as these sections are populated).