【问题标题】:How to change the background color of UITableViewController to gradient color on swift 3如何在swift 3上将UITableViewController的背景颜色更改为渐变色
【发布时间】:2017-03-25 17:29:47
【问题描述】:

我想将我的 UITableViewController 背景颜色更改为渐变色。

我的代码如下:

override func viewDidLoad() {
    super.viewDidLoad()

    let gradient = CAGradientLayer()
    let gradientLocations = [0.0,1.0]

    gradient.frame = view.bounds;
    gradient.colors = [primaryColor, secondaryColor]
    gradient.locations = gradientLocations as [NSNumber]?

    let backgroundView = UIView(frame: view.bounds)
    backgroundView.layer.insertSublayer(gradient, at: 0)
    tableView.backgroundView = backgroundView

    tableView.separatorStyle = UITableViewCellSeparatorStyle.none
    tableView.backgroundColor = UIColor.clear

}

然后变成黑色。

我发现了这个非常相似的问题,但它无法解决我的问题。 Set gradient behind UITableView

请帮忙! 提前致谢。

【问题讨论】:

  • 我的谷歌搜索...“swift 3 渐变背景”.. 最佳结果.. stackoverflow.com/questions/24380535/….虽然这不是专门针对表格视图的,但它应该可以解决您的一个问题……然后是所有问题。

标签: uitableview swift3


【解决方案1】:

我创建了一个带有完全包含您的代码的表格视图控制器的新项目。我所要做的就是使单元格也透明,就像你链接到的帖子所说的那样。它起作用了,所以这让我很好奇。您没有显示 primaryColorsecondaryColor 是什么。

请注意,您需要在gradient.colors 中使用CGColors。我刚刚尝试使用UIColors,屏幕也一样黑。尝试使用

gradient.colors = [UIColor.red.cgColor, UIColor.blue.cgColor]

或者更确切地说(如果它们确实是 UIColor 类型)

gradient.colors = [primaryColor.cgColor, secondaryColor.cgColor]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 2012-07-10
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    相关资源
    最近更新 更多