我有一个
我已经查看了一些答案,比如this,但似乎都不起作用。
在
但我看不到渐变效果,删除渐变代码后也没有区别。我的渐变是在图片下面吗?
如果我用
UITableView
,其中我的单元格backgroundView
是一个UIImageView
。我希望每个图像的顶部渐变为黑色,以便我可以叠加一些白色文本。我已经查看了一些答案,比如this,但似乎都不起作用。
在
tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
中,我尝试过:var imageView = UIImageView(image: image)
var gradient: CAGradientLayer = CAGradientLayer()
gradient.frame = imageView.frame
gradient.colors = [UIColor.blackColor(), UIColor.clearColor()]
gradient.locations = [0.0, 0.1]
imageView.layer.insertSublayer(gradient, atIndex: 0)
cell!.backgroundView = imageView
但我看不到渐变效果,删除渐变代码后也没有区别。我的渐变是在图片下面吗?
如果我用
imageView.layer.mask = gradient
替换imageView.layer.insertSublayer(gradient, atIndex: 0)
这行代码,那么我的单元格只会是空白和白色(没有图片了)。
gradient.colors
应该包含CGColor
而不是UIColor
。 - duci9y