1

I am using swift 2.2 .I have a view and i want to use gradient color on inner border sides of view.Please help me.I am using the following code.Here i attach the screenshot which i need view.

self.layer.borderWidth = 1 

self.layer.borderColor = UIColor(white:0.1, alpha: 0.5).CGColor

enter image description here

3 Answers 3

0

try this code

@IBOutlet weak var gradientView: UIView!

        gradientView.backgroundColor = UIColor.clearColor()
        let gradient:CAGradientLayer = CAGradientLayer()

        gradient.startPoint = CGPoint(x: 0.0, y: 1.0)
        gradient.endPoint = CGPoint(x: 0.0, y: 0.0)
        gradient.frame.size = self.gradientView.frame.size
        gradient.colors = [UIColor.darkGrayColor().CGColor,UIColor.darkGrayColor().colorWithAlphaComponent(0).CGColor] //Or any colors
        gradient.locations = [ 0.0, 1.0]

        self.gradientView.layer.insertSublayer(gradient, atIndex: 0)
0

iPatel's answer would work, but if you want to use the gradient function rather than an image, you could simply place a view with a gradient fill behind a slightly smaller view that holds your content. As long as the smaller, front view is inset from the gradient view by the same distance on all sides then this will give the effect that you are looking for.

1
  • Personally, I wouldn't write code for this. I'd use interface builder and pin the front view within the view that has the gradient. If you want to do it programmatically then the code will depend on your exact use case. There are lots of online tutorials that will get you up to speed on defining the gradient.
    – Westside
    Jul 7, 2016 at 9:26
0

As per my suggestion use below code for achieve gradient color

self.layer.borderColor = UIColor(patternImage:UIImage(named: "myCorrectImgName.png")!).CGColor

Where "myCorrectImgName.png" is name of image that have gradient color or required image that you want.

Otherwise you will be find may examples from the Google for how to set gradient color.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.