Created
May 10, 2020 05:46
-
-
Save Aman2306/ad62bb5c7398da7cd02820d723e6fda5 to your computer and use it in GitHub Desktop.
Custom Gradient View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
@IBDesignable // this means that it will work inside storyboard, means it will render storyboard | |
class GradientView: UIView { | |
@IBInspectable var topColor: UIColor = #colorLiteral(red: 0.2980392157, green: 0.3019607843, blue: 0.8588235294, alpha: 1) { | |
didSet { | |
self.setNeedsLayout() | |
} | |
} | |
@IBInspectable var bottomColor: UIColor = #colorLiteral(red: 0.1725490196, green: 0.831372549, blue: 0.8588235294, alpha: 1) { | |
didSet { | |
self.setNeedsLayout() | |
} | |
} | |
override func layoutSubviews() { | |
let gradientLayer = CAGradientLayer() | |
gradientLayer.colors = [topColor.cgColor, bottomColor.cgColor] | |
gradientLayer.startPoint = CGPoint(x: 0,y: 0) | |
gradientLayer.endPoint = CGPoint(x: 1, y: 1) | |
gradientLayer.frame = self.bounds | |
self.layer.insertSublayer(gradientLayer, at: 0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment