Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I bring a UIView's border behind a UIImage?

I'm new to Swift and I'm facing an issue while trying to develop a small application and I really hope someone will help me out.

Basically, I have a UIImage inside a UIView, and I want to send the UIView's border to the back (See picture below)

border in front of image

So, I'm giving the UIView a border, and it goes through my image. My question is:

How do I bring the border of the UIView behind the UIImage?

Note: I apologize if this is a repeated question, I was looking for answers for hours but couldn't find what I need.

Best regards!

Here is my code for the Image:

speakerImage.layer.cornerRadius = speakerImage.frame.size.width / 2;
speakerImage.clipsToBounds = true;
speakerImage.layer.zPosition = 1

And here is my code for the View:

speakerContentView.layer.cornerRadius = 5
speakerContentView.layer.borderWidth = 1
speakerContentView.layer.borderColor = UIColor.gray.cgColor
speakerContentView.layer.shadowColor = UIColor.gray.cgColor
speakerContentView.layer.shadowOpacity = 0.7
speakerContentView.layer.shadowOffset = CGSize(width: 0, height: 3)
speakerContentView.layer.zPosition = -1
like image 447
Comrade Avatar asked Jan 30 '18 14:01

Comrade


2 Answers

You should add a subview with the same size of the superview below the image, and assign the border/shadow to it instead of its superview.

view hierarchy

like image 20
Tamás Sengel Avatar answered Nov 07 '22 12:11

Tamás Sengel


This may help you!!

  • Move your image outside current (speakerContentView) view and set it in higher hierarchy so that it can get front position.
  • Set vertical center Y of image equal to current (speakerContentView) view Y

Look at this image:

enter image description here

May be not a standard way but will solve your problem.

like image 192
Krunal Avatar answered Nov 07 '22 12:11

Krunal