I'm trying to add a gradient to a view but it doesn't work. This is the code:
class TermsAndConditionViewController: UIViewController, NavigationBarToggling {
@IBOutlet weak var tncContentView: UIView!
@IBOutlet weak var tncTextView: UITextView!
@IBOutlet weak var declineButton: AeonButton!
@IBOutlet weak var acceptButton: AeonButton!
@IBOutlet weak var unreadIndicatorView: UIView!
@IBOutlet weak var unreadLabel: UILabel!
let isNavigationBarHidden: Bool = false
let disposeBag = DisposeBag()
lazy var segueManager: SegueManager = {
return SegueManager(viewController: self)
}()
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
segueManager.prepare(for: segue)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setupViews()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
title = "Terms & Conditions"
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setupGradientUnreadIndicator()
}
private func setupViews() {
tncContentView.addCorner(radius: 8)
tncContentView.addBorder(width: 1, color: UIColor.black.withAlphaComponent(0.1).cgColor)
}
private func setupGradientUnreadIndicator() {
let gradient = CAGradientLayer()
gradient.type = .axial
gradient.colors = [
R.color.vcSubBgColor()!.withAlphaComponent(0),
UIColor.white
]
gradient.locations = [0, 1]
gradient.startPoint = CGPoint(x: 1, y: 0)
gradient.startPoint = CGPoint(x: 1, y: 1)
gradient.frame = unreadIndicatorView.bounds
unreadIndicatorView.layer.insertSublayer(gradient, at: 0)
}
}
As you can see, the view at the bottom of the Text View is still solid. I was expecting it will go from transparent to white. Can you help me?
Thanks.
PS: BTW, the reason I call the setupGradientUnreadIndicator()
in viewDidAppear()
is because my whole scene is using auto-layout. So I put it there when the frame of the view is already calculated.
As mentioned by @Larme in the comment, it is because I used UIColor
instead of CGColor
in the gradient.colors
. After I changed it to CGColor
it worked fine now.