Hello Everyone I want to add Gradient Background Color to UIView using UIAppearnce() with CustomView Class.I am facing an error - Thread 1: " Illegal axis type, @, for appearance setter, setBorderGradientColortoView1WithFirstColor:secondColor:. Expected NSInteger or NSUInteger". I attached my code .
import Foundation
import UIKit
import SwiftHEXColors
class CustomUIViewClass: UIView {
@objc dynamic var subviewColor: UIColor? {
get { return self.backgroundColor }
set { self.backgroundColor = newValue }
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setup()
}
@objc dynamic func setup(){
self.backgroundColor = .yellow
self.frame.size.height = 250
self.layer.borderWidth = 5
self.backgroundColor = subviewColor
}
@objc dynamic func setBorderGradientColortoView1(firstColor : String , secondColor : String) {
let colorTop = UIColor(hexString : firstColor )!.cgColor
let colorBottom = UIColor(hexString : secondColor)!.cgColor
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [colorTop, colorBottom]
gradientLayer.locations = [0.0, 1.0]
gradientLayer.frame = self.bounds
gradientLayer.cornerRadius = self.layer.cornerRadius
self.layer.sublayers?.filter{ $0 is CAGradientLayer }.forEach{ $0.removeFromSuperlayer() }
self.layer.insertSublayer(gradientLayer, at:0)
}
}
//Setting the value in View Controller CustomUIViewClass.appearance().setBorderGradientColortoView1(firstColor: "#FF2222", secondColor: "#7A1111")
//Error Thread 1: "*** Illegal axis type, @, for appearance setter, setBorderGradientColortoView1WithFirstColor:secondColor:. Expected NSInteger or NSUInteger"