I have created a progress bar to be used in a tableView by creating a gradient layer. It works perfectly.
iPhone5:
As an alternative to the accepted answer, you could also change the views layer class to be CAGradientLayer
. The views layer will always be resized according to layout changes. You can achieve that by subclassing UIView
class GradientView: UIView {
override class func layerClass() -> AnyClass {
return CAGradientLayer.self
}
}
then set the colors
if let gradientLayer = gradientView.layer as? CAGradientLayer {
gradientLayer.colors = arrayColors
}
It's less code than adding and maintaining a sublayer, but might not suit all use cases.