【问题标题】:Cannot insert CAGradientLayer at index 0 (behind tableView)无法在索引 0 处插入 CAGradientLayer(在 tableView 后面)
【发布时间】:2017-11-09 07:11:41
【问题描述】:

尝试插入具有自定义渐变的图层作为 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 中实现此代码。

同样的事情......

【问题讨论】:

    标签: ios swift xcode uitableview cagradientlayer


    【解决方案1】:

    找到了另一种使用 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
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-04
      • 2020-06-10
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-01
      • 1970-01-01
      相关资源
      最近更新 更多