表格视图的渐变背景视图

wydwbb8l  于 2022-10-04  发布在  Swift
关注(0)|答案(2)|浏览(136)

我想要为我的表视图使用渐变背景,并使用以下方法成功地实现了:

private func setBackgroundViewColor() {
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = [#colorLiteral(red: 0.0923477529, green: 0.1613951182, blue: 0.3891475176, alpha: 1).cgColor, #colorLiteral(red: 0.1546722437, green: 0.02495219428, blue: 0.2035871579, alpha: 1).cgColor]
        gradientLayer.locations = [NSNumber(value: 0.0), NSNumber(value: 1.0)]

        gradientLayer.frame = tableView.bounds
        let backgroundView = UIView(frame: tableView.bounds)
        backgroundView.layer.addSublayer(gradientLayer)
        tableView.backgroundView = backgroundView
    }

但这仅适用于带有情节提要的标准单元格,如果我尝试注册自定义单元格

tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellID)

然后,不会出现渐变,我会得到以下结果

怎么才能解决呢?

k0pti3hp

k0pti3hp1#

渐变是单元格后面的蓝色吗?自定义单元格看起来有不同的背景颜色。可能不是最佳解决方案,但您可以在ViewDidLoad中尝试。因此,您的TableView渐变颜色应该通过单元格显示出来。

UITableViewCell.self.appearance().backgroundColor = UIColor.clear
x6492ojm

x6492ojm2#

结果是,您需要在viewWillEmerar中调用函数setBackround ViewColor(),然后一切都会正常工作。

相关问题