background Gradient : linear left to right in objective C

 enum GradiantDirection {
        case leftToRight
        case rightToLeft
        case topToBottom
        case bottomToTop
    }
    
 class  func setGradiantColor(view : UIView, topColor : UIColor, bottomColor:UIColor, cornerRadius : CGFloat = 0.0,gradiantDirection : GradiantDirection = .topToBottom )
    {
        
        view.layer.sublayers?.filter{ $0 is CAGradientLayer }.forEach{ $0.removeFromSuperlayer() }
        
        let gradient: CAGradientLayer = CAGradientLayer()
        gradient.colors = [topColor.cgColor,bottomColor.cgColor]
        gradient.frame = view.bounds
        
        switch gradiantDirection {
        case .topToBottom:
            gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
            gradient.endPoint = CGPoint(x: 0.0, y: 1.0)
        case .bottomToTop:
            gradient.startPoint = CGPoint(x: 1.0, y: 0.5)
            gradient.endPoint = CGPoint(x: 0.0, y: 0.5)
        case .leftToRight:
            gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
            gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
        case .rightToLeft:
            gradient.startPoint = CGPoint(x: 1.0, y: 0.5)
            gradient.endPoint = CGPoint(x: 0.0, y: 0.5)
        }
        
        gradient.masksToBounds = true
        let gradientLayer = CAGradientLayer()
        gradientLayer.cornerRadius = cornerRadius
        gradient.rasterizationScale = 100
        view.layer.insertSublayer(gradient, at: 0)
    }

You need to set the startPoint and endPoint property of your gradientLayer. They represent the start coordinates of your first color and the coordinates of the end of your last color.

They are both CGPoints and their x and y should have values between 0.0 et 1.0.

By default the startPoint has these coordinates (0.5, 0.0), while the endPoint has those (0.5, 1.0).

(0.0, 0.0) is the top left corner while (1.0, 1.0) is the bottom right corner

so try:

theViewGradient.startPoint = CGPointMake(0.0, 0.5);
theViewGradient.endPoint = CGPointMake(1.0, 0.5);

 **The start and end points of the gradient when drawn into the layer's
 coordinate space. The start point corresponds to the first gradient
 stop, the end point to the last gradient stop. Both points are
 defined in a unit coordinate space that is then mapped to the
 layer's bounds rectangle when drawn. (i.e. [0,0] is the bottom-left
 corner of the layer, [1,1] is the top-right corner.).The default values
 are [.5,0] and [.5,1] respectively.** 

theViewGradient.startPoint = CGPointMake(0.0, 0.5);
theViewGradient.endPoint = CGPointMake(1.0, 0.5);

In Swift 3.0 & 4.0, to go from left to right:

    gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
    gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)