1

I just simply want to apply Gradient with 2 UIColors that's why I written below code.

class Constants: NSObject {
    class func applyGradient(localView:UIView, color1:UIColor, color2:UIColor) {
        let gradient: CAGradientLayer = CAGradientLayer()
        gradient.frame = localView.bounds
        gradient.colors = [color1.CGColor, color2.CGColor]
        gradient.startPoint = CGPoint(x: 0,y: 0)
        gradient.endPoint = CGPoint(x: 0,y: 1)
        localView.layer.insertSublayer(gradient, atIndex: 0)
        localView.layer.masksToBounds = true
    }
}

and applying via below code in UIViewController's viewDidLoad.

Constants.applyGradient(viewContactDetail, color1: gradientColor1, color2: gradientColor2)
Constants.applyGradient(viewDiscountDetail, color1: gradientColor1, color2: gradientColor2)
Constants.applyGradient(viewTermsCondDetail, color1: gradientColor1, color2: gradientColor2)
Constants.applyGradient(viewDiscountAvailDetail, color1: gradientColor1, color2: gradientColor2)

And here is the result what I'm getting

enter image description here

You can see, Gradient isn't apply on specific view properly.
I'm not getting what is wrong with my code.

EDIT: Here is the hierarchical view.
enter image description here

7
  • You'r code is proper. Please try it with basic UIColor. Commented Oct 13, 2016 at 5:23
  • @Mrugesh Tank check my answer Commented Oct 13, 2016 at 5:52
  • @Mrugesh Tank you want two colors in gradient for particular view? Commented Oct 13, 2016 at 6:02
  • @AnkitaShah, I'm using Basic UIcolor, like gradientColor1 have white color and gradientColor2 have some RGB color which looks like grey Commented Oct 13, 2016 at 6:15
  • I just copied you'r code in one of my project and it's working properly Commented Oct 13, 2016 at 6:31

3 Answers 3

2

Try this....hope its helpful for you

let gradientLayer = CAGradientLayer()

    gradientLayer.frame = self.shadowview.bounds

    let colorTop = UIColor.green.cgColor
    let colorBottom = UIColor.black.cgColor

    gradientLayer.colors = [colorTop, colorBottom]
    gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
    gradientLayer.endPoint = CGPoint(x: 0.0, y: 1.0)
    gradientLayer.locations = [ 0.0, 1.0]


    self.shadowview.layer.addSublayer(gradientLayer)

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

So, I just need to add .locations property??
@MrugeshTank u get solution?
exactly which type of gradient layer u want?
gradient layer is proper how I want but, it's height is not proper, Cause I have each view with border . You can see in screenshot where borders are there and where gradient layer ends.
hey @MrugeshTank prntscr.com/cv9bm5 i have created view like this please see and tell me, i will give you demo
|
0

You can try this simple code too in which you can give frame of gradient layer and give color too

let gradient1 = CAGradientLayer()
gradient1.frame =  CGRect(origin: CGPointZero, size:  CGSize (width: self.view.frame.size.width,height: btn_cancel.frame.size.height))
gradient1.colors = [UIColor.whiteColor().CGColor, UIColor.init(red: 237.0/255.0, green: 237.0/255.0, blue: 237.0/255.0, alpha: 1.0).CGColor]
print(gradient1.frame)

just Add that layer to the view

 "YOUR VIEW".layer.addSublayer(gradient1)

In Image You can see I added gradient layer to button, So you can do as same to add in your view or any other control

Comments

0

Calling layoutIfNeeded() before setting the frame worked for me. If in your case if don't want to call this try using willDisplayCell by setting a bool which will be applied for the first time only and when your frames are ready.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.