Skip to content

Instantly share code, notes, and snippets.

@ozgurshn
Created February 27, 2017 23:24
Show Gist options
  • Save ozgurshn/f4fb762d7b64e410390ca2c772fcf0ae to your computer and use it in GitHub Desktop.
Save ozgurshn/f4fb762d7b64e410390ca2c772fcf0ae to your computer and use it in GitHub Desktop.
IBInspectable for Rounded and Gradient UIView
import UIKit
@IBDesignable
class CustomView: UIView {
var fromColor: UIColor?
var toColor: UIColor?
var gradientLayer: CAGradientLayer?
@IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadius
layer.masksToBounds = cornerRadius > 0
}
}
@IBInspectable var topColor: UIColor? {
didSet {
fromColor = topColor
setGradient()
}
}
@IBInspectable var bottomColor: UIColor? {
didSet {
toColor = bottomColor
setGradient()
}
}
func setGradient() {
let gradientLocations = [0.0,1.0]
let gradientLayer = CAGradientLayer()
if fromColor != nil && toColor != nil
{
gradientLayer.colors = [fromColor!.cgColor, toColor!.cgColor]
}
gradientLayer.locations = gradientLocations as [NSNumber]
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