无法在索引 0 处插入 CAGradientLayer(在 tableView 后面)

Cannot insert CAGradientLayer at index 0 (behind tableView)

正在尝试插入一个带有自定义渐变的图层作为 table 视图的背景。但是显示在table.

前面

以前可以用,现在更新了 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)

有些选项根本不显示渐变,有些选项显示在 table 的前面。

class本身就是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

但是,仍然不明白为什么以前的解决方案停止工作...