admin管理员组文章数量:1023795
I have an app with a UITableView. We're gradually converting to SwiftUI, so the UITableView is basically a collection of SwiftUI views. I was able to get the background to appear as a gradient using these two functions:
func createBackgroundGradient() -> CAGradientLayer {
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [UIColor.red.cgColor, UIColor.white.cgColor, UIColor.red.cgColor]
gradientLayer.locations = [0.0, 0.5, 1.0]
gradientLayer.frame = self.tableView.bounds
return gradientLayer
}
func refreshBackground() {
tableView.backgroundView = nil
let backgroundView = UIView()
let gradientView = createBackgroundGradient()
backgroundView.layer.addSublayer(gradientView)
tableView.backgroundView = backgroundView
}
I call this function from viewWillLayoutSubview because I have views popping in as we get data back from the back end. This works to set the gradient the length of the screen. However, I want the gradient to take up the entire UITableView, and I want it to scroll with the table view (which is much longer than the length of the screen), not just sit in the background with the cells scrolling on top of it.
The solution to this question: Gradient background color with UIScrollView looks like the behavior I want, but when I change my refreshBackround function to this:
func refreshBackground() {
if let gradientSublayer = self.gradientSublayer {
gradientSublayer.removeFromSuperlayer()
}
self.gradientSublayer = createBackgroundGradient()
tableView.layer.insertSublayer(self.gradientSublayer ?? createBackgroundGradient(), at: 0)
}
I don't see any difference in behavior - it's still just the length of the screen and it doesn't change position when I scroll.
Does anyone know of a way around this? I've thought about trying to color the different sections with different portions of the gradient, but that seems like it'll get really complicated because the various sections in the table may or may not be there based on the data we're getting from the back end (and it'll probably look janky because portions of the gradient will be popping in as those sections are populated).
I have an app with a UITableView. We're gradually converting to SwiftUI, so the UITableView is basically a collection of SwiftUI views. I was able to get the background to appear as a gradient using these two functions:
func createBackgroundGradient() -> CAGradientLayer {
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [UIColor.red.cgColor, UIColor.white.cgColor, UIColor.red.cgColor]
gradientLayer.locations = [0.0, 0.5, 1.0]
gradientLayer.frame = self.tableView.bounds
return gradientLayer
}
func refreshBackground() {
tableView.backgroundView = nil
let backgroundView = UIView()
let gradientView = createBackgroundGradient()
backgroundView.layer.addSublayer(gradientView)
tableView.backgroundView = backgroundView
}
I call this function from viewWillLayoutSubview because I have views popping in as we get data back from the back end. This works to set the gradient the length of the screen. However, I want the gradient to take up the entire UITableView, and I want it to scroll with the table view (which is much longer than the length of the screen), not just sit in the background with the cells scrolling on top of it.
The solution to this question: Gradient background color with UIScrollView looks like the behavior I want, but when I change my refreshBackround function to this:
func refreshBackground() {
if let gradientSublayer = self.gradientSublayer {
gradientSublayer.removeFromSuperlayer()
}
self.gradientSublayer = createBackgroundGradient()
tableView.layer.insertSublayer(self.gradientSublayer ?? createBackgroundGradient(), at: 0)
}
I don't see any difference in behavior - it's still just the length of the screen and it doesn't change position when I scroll.
Does anyone know of a way around this? I've thought about trying to color the different sections with different portions of the gradient, but that seems like it'll get really complicated because the various sections in the table may or may not be there based on the data we're getting from the back end (and it'll probably look janky because portions of the gradient will be popping in as those sections are populated).
Share Improve this question asked Nov 18, 2024 at 21:49 kbartlettkbartlett 1601 silver badge8 bronze badges1 Answer
Reset to default 0In order to have gradientLayer
take up entirely UITableView
. You need to subcribe to tableView
's bounds change and refreshBackground
base on new bounds
. Here is a way using Combine
:
class ViewController: UITableViewController {
private var cancels = Set<AnyCancellable>()
override func viewDidLoad() {
super.viewDidLoad()
tableView.publisher(for: \.bounds)
.removeDuplicates()
.sink { [weak self] size in
self?.createBackgroundGradient()
}
.store(in: &cancels)
}
var gradientLayer: CAGradientLayer!
func createBackgroundGradient() {
if let gradientSublayer = self.gradientLayer {
gradientSublayer.removeFromSuperlayer()
}
gradientLayer = CAGradientLayer()
gradientLayer.colors = [UIColor.red.cgColor, UIColor.white.cgColor, UIColor.red.cgColor]
gradientLayer.locations = [0.0, 0.5, 1.0]
gradientLayer.zPosition = -1000
gradientLayer.frame = self.tableView.bounds
tableView.layer.insertSublayer(self.gradientLayer, at: 0)
}
}
I have an app with a UITableView. We're gradually converting to SwiftUI, so the UITableView is basically a collection of SwiftUI views. I was able to get the background to appear as a gradient using these two functions:
func createBackgroundGradient() -> CAGradientLayer {
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [UIColor.red.cgColor, UIColor.white.cgColor, UIColor.red.cgColor]
gradientLayer.locations = [0.0, 0.5, 1.0]
gradientLayer.frame = self.tableView.bounds
return gradientLayer
}
func refreshBackground() {
tableView.backgroundView = nil
let backgroundView = UIView()
let gradientView = createBackgroundGradient()
backgroundView.layer.addSublayer(gradientView)
tableView.backgroundView = backgroundView
}
I call this function from viewWillLayoutSubview because I have views popping in as we get data back from the back end. This works to set the gradient the length of the screen. However, I want the gradient to take up the entire UITableView, and I want it to scroll with the table view (which is much longer than the length of the screen), not just sit in the background with the cells scrolling on top of it.
The solution to this question: Gradient background color with UIScrollView looks like the behavior I want, but when I change my refreshBackround function to this:
func refreshBackground() {
if let gradientSublayer = self.gradientSublayer {
gradientSublayer.removeFromSuperlayer()
}
self.gradientSublayer = createBackgroundGradient()
tableView.layer.insertSublayer(self.gradientSublayer ?? createBackgroundGradient(), at: 0)
}
I don't see any difference in behavior - it's still just the length of the screen and it doesn't change position when I scroll.
Does anyone know of a way around this? I've thought about trying to color the different sections with different portions of the gradient, but that seems like it'll get really complicated because the various sections in the table may or may not be there based on the data we're getting from the back end (and it'll probably look janky because portions of the gradient will be popping in as those sections are populated).
I have an app with a UITableView. We're gradually converting to SwiftUI, so the UITableView is basically a collection of SwiftUI views. I was able to get the background to appear as a gradient using these two functions:
func createBackgroundGradient() -> CAGradientLayer {
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [UIColor.red.cgColor, UIColor.white.cgColor, UIColor.red.cgColor]
gradientLayer.locations = [0.0, 0.5, 1.0]
gradientLayer.frame = self.tableView.bounds
return gradientLayer
}
func refreshBackground() {
tableView.backgroundView = nil
let backgroundView = UIView()
let gradientView = createBackgroundGradient()
backgroundView.layer.addSublayer(gradientView)
tableView.backgroundView = backgroundView
}
I call this function from viewWillLayoutSubview because I have views popping in as we get data back from the back end. This works to set the gradient the length of the screen. However, I want the gradient to take up the entire UITableView, and I want it to scroll with the table view (which is much longer than the length of the screen), not just sit in the background with the cells scrolling on top of it.
The solution to this question: Gradient background color with UIScrollView looks like the behavior I want, but when I change my refreshBackround function to this:
func refreshBackground() {
if let gradientSublayer = self.gradientSublayer {
gradientSublayer.removeFromSuperlayer()
}
self.gradientSublayer = createBackgroundGradient()
tableView.layer.insertSublayer(self.gradientSublayer ?? createBackgroundGradient(), at: 0)
}
I don't see any difference in behavior - it's still just the length of the screen and it doesn't change position when I scroll.
Does anyone know of a way around this? I've thought about trying to color the different sections with different portions of the gradient, but that seems like it'll get really complicated because the various sections in the table may or may not be there based on the data we're getting from the back end (and it'll probably look janky because portions of the gradient will be popping in as those sections are populated).
Share Improve this question asked Nov 18, 2024 at 21:49 kbartlettkbartlett 1601 silver badge8 bronze badges1 Answer
Reset to default 0In order to have gradientLayer
take up entirely UITableView
. You need to subcribe to tableView
's bounds change and refreshBackground
base on new bounds
. Here is a way using Combine
:
class ViewController: UITableViewController {
private var cancels = Set<AnyCancellable>()
override func viewDidLoad() {
super.viewDidLoad()
tableView.publisher(for: \.bounds)
.removeDuplicates()
.sink { [weak self] size in
self?.createBackgroundGradient()
}
.store(in: &cancels)
}
var gradientLayer: CAGradientLayer!
func createBackgroundGradient() {
if let gradientSublayer = self.gradientLayer {
gradientSublayer.removeFromSuperlayer()
}
gradientLayer = CAGradientLayer()
gradientLayer.colors = [UIColor.red.cgColor, UIColor.white.cgColor, UIColor.red.cgColor]
gradientLayer.locations = [0.0, 0.5, 1.0]
gradientLayer.zPosition = -1000
gradientLayer.frame = self.tableView.bounds
tableView.layer.insertSublayer(self.gradientLayer, at: 0)
}
}
版权声明:本文标题:ios - SwiftUIKIt: How to make a gradient background take up the entire length of the UITableView? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745592178a2157955.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论