添加渐变以滚动UITableView [英] Adding gradient to scrolling UITableView
问题描述
我能够添加渐变我的UITableView,但我有一个问题,当我必须滚动我的单元格时,渐变背景也会滚动。当我向上或向下滚动时,我希望背景保持一致。我怎样才能做到这一点?我是否必须创建自定义UITableView才能执行此操作?
I was able to add a gradient my UITableView, but I have the issue of when I have to scroll through my cells, the gradient background scrolls along also. I want the background to stay consistent as I scroll up or down. How can I achieve this? Do I have to create a custom UITableView in order to do this?
下面的图片显示了目前的状态。
The pictures below show what it currently looks like.
这是我将梯度添加到UITableView的代码:
Here is my code for adding the gradient to the 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)
}
设置渐变也不起作用,如设置:
Messing around will setting the gradient doesn't work either, like setting:
self.view.layer.insertSublayer(gradient, atIndex: 0)
或更改边界:
gradient.frame = self.tableView.frame
此外,在 cellForRowAtIndexPath
我将UITableViewCells背景颜色设置为清除:
Also, in cellForRowAtIndexPath
I set the UITableViewCells background color to clear:
cell.backgroundColor = UIColor.clearColor()
我无法添加图片,但如果您希望看到它们,请点击链接: http:// imgur。 com / Vtka1tO,6faLMkr#0
I can't add images, but here is the link if you wish to see them: http://imgur.com/Vtka1tO,6faLMkr#0
推荐答案
如果不这样做,渐变需要在表格视图后面想要滚动。如果您使用的是UITableViewController,那么唯一的背后是窗口,因此您可以为其指定渐变,并使单元格和表格视图具有清晰的背景颜色。如果您正在使用带有表视图的UIViewController作为子视图,那么您可以为控制器的主视图提供渐变背景颜色。
The gradient needs to be behind the table view if you don't want it to scroll. If you're using a UITableViewController, the only thing behind is the window, so you could give it the gradient, and make the cells and the table view have a clear background color. If you're using a UIViewController with a table view as a subview, then you could give the controller's main view a gradient background color.
这篇关于添加渐变以滚动UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!