尝试插入具有自定义渐变的图层作为 tableView 的背景。但是,它显示在表格前面。
它以前可以工作,但现在当我更新 XCode 后就不再工作了。
let statusBarHeight = UIApplication.shared.statusBarFrame.height
let gradient = self.getMenuGradientLayer()
gradient.frame = CGRect(x: 0, y: -statusBarHeight, width: self.view.frame.width, height: statusBarHeight + self.view.bounds.height)
// Tried multiple options. Not at the same time, of course.
self.view.layer.insertSublayer(gradient, at: 0) // Worked previously
self.tableView.layer.insertSublayer(gradient, at: 0)
self.tableView.backgroundView?.layer.insertSublayer(gradient, at: 0)
其中一些选项根本不显示渐变,有些选项将其显示在表格的前面。
类本身是UITableViewController。
我尝试在viewDidLoad、viewWillAppear、viewWillLayoutSubviews 中实现此代码。
同样的事情...
最佳答案
找到了另一种使用tableView的backgroundView属性来执行任务的方法。
let statusBarHeight = UIApplication.shared.statusBarFrame.height
let frame = CGRect(x: 0, y: -statusBarHeight, width: self.view.frame.width, height: statusBarHeight + self.view.bounds.height)
let gradient = self.getMenuGradientLayer()
gradient.frame = frame
let backgroundView = UIView(frame: frame)
backgroundView.layer.insertSublayer(gradient, at: 0)
self.tableView.backgroundView = backgroundView
但是,仍然不明白为什么以前的解决方案停止工作......
关于ios - 无法在索引 0 处插入 CAGradientLayer(在 tableView 后面),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47195938/