Waruna
Waruna

Reputation: 1174

Add a complex boarder to UIImageView

How do I add boarder like in the below image (in Objective-C ) to UIImageView. I tried using UIBezierPath but wasn't successful. enter image description here

Upvotes: 0

Views: 44

Answers (1)

Saurabh Bhatia
Saurabh Bhatia

Reputation: 1925

  1. Create a UIView (square in dimensions), add corner radius = 1/2 times its side.

  2. Now add the UIImageView as a subview to this UIView.

As the image is circular in shape, so a simpler appraoch to the solution to your problem is to add corner radius to your UIVIew.

Below is the code I used:

    let circleView: UIView = UIView(frame: CGRect(x: 0,
                                                  y: 0,
                                                  width: 100,
                                                  height: 100))
    circleView.center = view.center
    circleView.backgroundColor = .lightGray
    circleView.layer.cornerRadius = 50
    view.addSubview(circleView)
    

    let imageView: UIImageView = UIImageView(frame: CGRect(x: 0, 
                                                           y: 0, 
                                                           width: 100, 
                                                           height: 100))
    imageView.image = UIImage(named: "F8FIs.png")
    circleView.addSubview(imageView)

Please note that I added lightGray color to circle view for clarity.

And here is the screenshot of how it looks:

enter image description here

Upvotes: 1

Related Questions