For my application I\'m using a TableView and using customized UITableViewCells.
I customized my cells via interface builder, not programmatically. Is there a way to als
answer by etayluz works fine but I added a couple changes:
Remove Gradient layer on every draw, so that it does not keep drawing and adding a new layer when redraw is necessary (for instance by calling .setNeedsDisplay()
on rotation).
@IBDesignable final class MFGradientView: UIView {
@IBInspectable var startColor: UIColor = UIColor.clear
@IBInspectable var endColor: UIColor = UIColor.clear
override func draw(_ rect: CGRect) {
layer.sublayers?.first?.removeFromSuperlayer()
let gradient: CAGradientLayer = CAGradientLayer()
gradient.frame = CGRect(origin: CGPoint.zero, size: self.bounds.size)
gradient.colors = [startColor.cgColor, endColor.cgColor]
layer.insertSublayer(gradient, at: 0)
}
}