gpt4 book ai didi

ios - 向滚动 UITableView 添加渐变

转载 作者:搜寻专家 更新时间:2023-11-01 06:23:24 26 4
gpt4 key购买 nike

我能够在我的 UITableView 中添加渐变,但我遇到了一个问题,即当我必须滚动我的单元格时,渐变背景也会随之滚动。我希望背景在我向上或向下滚动时保持一致。我怎样才能做到这一点?我是否必须创建自定义 UITableView 才能执行此操作?

下面的图片显示了它当前的样子。

这是我将渐变添加到 UITableView 的代码:

func addGradientToBackground(){
var gradient : CAGradientLayer = CAGradientLayer()
gradient.frame = self.tableView.bounds
gradient.colors = [UIColor.blueColor().CGColor, UIColor.redColor().CGColor]
self.tableView.layer.insertSublayer(gradient, atIndex: 0)
}

乱来设置渐变也不行,比如设置:

self.view.layer.insertSublayer(gradient, atIndex: 0)

或改变边界:

gradient.frame = self.tableView.frame

此外,在 cellForRowAtIndexPath 中,我将 UITableViewCells 背景颜色设置为清除:

cell.backgroundColor = UIColor.clearColor()

我无法添加图片,但如果您想查看它们,请点击此处链接:http://imgur.com/Vtka1tO,6faLMkr#0

最佳答案

如果你不希望它滚动,渐变需要在 TableView 后面。如果你使用的是 UITableViewController,后面唯一的东西就是窗口,所以你可以给它渐变,并使单元格和表格 View 具有清晰的背景色。如果您使用带有 TableView 的 UIViewController 作为 subview ,那么您可以为 Controller 的主视图提供渐变背景颜色。

关于ios - 向滚动 UITableView 添加渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29241563/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com