Logan
Logan

Reputation: 1212

Multiple CALayer borders on UIImage

I am trying to add two different border layers to my image.

I have added the first with the following code:

myImage.layer.borderColor = UIColor.blue.cgColor myImage.layer.borderWidth = 2.0 myImage.roundLayerCorners()

When I do something like:

let sublayer: CALayer = CALayer() sublayer.backgroundCOlor = UIColor.white.cgColor sublayer.borderWith = 4.0 myImage.layer.addSublayer(sublayer)

It doesn't show up at all. Why, and how would I add multiple borders?

I want a 2 pixel wide blue border followed by a 2 pixel wide white border

Thank you!

Upvotes: 1

Views: 713

Answers (1)

Jacolack
Jacolack

Reputation: 1508

Your sublayer does not have a frame. This means it does not know how big to make itself. You can solve this by adding one line of code:

sublayer.frame = CGRect(x:2, y: 2, width: myImage.frame.width - 4, height: myImage.frame.height - 4)

So that your setup of your sublayer would look like this:

let sublayer: CALayer = CALayer()
sublayer.backgroundColor = UIColor.white.cgColor
sublayer.borderWidth = 4.0
sublayer.frame = CGRect(x:2, y: 2, width: myImage.frame.width - 4, height: myImage.frame.height - 4)
myImage.layer.addSublayer(sublayer)

Upvotes: 2

Related Questions