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

标签 ios swift xcode uitableview cagradientlayer

尝试插入具有自定义渐变的图层作为 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/

相关文章:

iphone - 使用选项卡 View 启动应用程序但未选择选项卡?

ios - 带有标题和图像的 UIButton

c++ - OpenGL:glGenVertexArrays() 给出 EXC_BAD_ACCESS

ios - func setupSession() { session = AVCaptureSession() session.sessionPreset = AVCaptureSessionPresetPhoto

ios - Int 不可转换为 DictionaryIndex<String, String>

ios - 在uicollectionview(单选)中设置对所选单元格的影响ios swift

ios - TableView (_ :editActionsForRowAt:) is not called on iOS 9. 3

iOS动画不保持最后位置

ios - 在非测试目标中使用 XCTest

ios - 登录 FB 用户的相册