'How to add gradient color to UIView using Custom Class and UIAppearnace() in swift

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"



Solution 1:[1]

From Apple's docs:

The property type may be any standard iOS type: id, NSInteger, NSUInteger, CGFloat, CGPoint, CGSize, CGRect, UIEdgeInsets or UIOffset. Axis parameter values must be either NSInteger or NSUInteger. UIKit throws an exception if other types are used in the axes.

You're trying to use String

You could change your func to:

@objc dynamic func setBorderGradientColortoView1(firstColor: Int, secondColor: Int)  {
    
    let c1 = UIColor(red: CGFloat((firstColor >> 16) & 0xff) / 255.0, green: CGFloat((firstColor >> 8) & 0xff) / 255.0, blue: CGFloat(firstColor & 0xff) / 255.0, alpha: 1.0)
    let c2 = UIColor(red: CGFloat((secondColor >> 16) & 0xff) / 255.0, green: CGFloat((secondColor >> 8) & 0xff) / 255.0, blue: CGFloat(secondColor & 0xff) / 255.0, alpha: 1.0)

    let colorTop = c1.cgColor
    let colorBottom = c2.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)
    
}

and then call it with:

CustomUIViewClass.appearance().setBorderGradientColortoView1(firstColor: 0xff2222, secondColor: 0x7a1111)

If you're getting your values as "#000000" format strings, first convert the string to an Int and pass that value.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DonMag