Apply gradient to UIButton iOS swift

 Solution:

extension UIButton {

    func applyGradient(colors: [CGColor]) {

        let gradientLayer = CAGradientLayer()

        gradientLayer.colors = colors

        gradientLayer.startPoint = CGPoint(x: 0.5, y: 0)

        gradientLayer.endPoint = CGPoint(x: 0.5, y: 1)

        gradientLayer.frame = self.bounds

        self.layer.insertSublayer(gradientLayer, at: 0)

    }

    

}


extension UIButton {

    func setBlackGrdient() {

        self.applyGradient(colors: [UIColor().colorWithHexString(hexString: "#252525").cgColor, UIColor().colorWithHexString(hexString: "#4e4e4e").cgColor])


    }

    

    func setWhiteGrdient() {

        self.applyGradient(colors: [UIColor().colorWithHexString(hexString: "#f4f4f4").cgColor, UIColor().colorWithHexString(hexString: "#b6b6b6").cgColor])


    }

}


The above function we can create button with black gradient using setBlackGrdient() function.


Also if you want to create with white gradient then use setWhiteGrdient() function

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift