0

I currently have three images next to each other. I combined them into a stack view and would like them to have rounded corners like so:

enter image description here

Instead, after setting the corner radius to each image to 5 I have this:

enter image description here

So my question is how I can make the second picture look like the first one? Keep in mind all three stars are in one stack view.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Faisal Syed
  • 921
  • 1
  • 11
  • 25
  • Try rounding the corners of the stack view, not the individual image views. – paulvs Oct 26 '16 at 18:04
  • 2
    I thought stack views can't have rounded corners – Faisal Syed Oct 26 '16 at 18:06
  • 2
    UIStackView just manages the position and size of its arranged views, the cornerRadius has no effect. http://stackoverflow.com/questions/33927914/how-can-i-set-the-cornerradius-of-a-uistackview You can add the UIView on top of StackView and then the Images on top of UIView. Try rounding the corners of UIView then. – Annie Gupta Oct 26 '16 at 18:07
  • You're right. Check this out: http://stackoverflow.com/a/5826698/1305067 – paulvs Oct 26 '16 at 18:09

3 Answers3

3

You can pick corners you want to round with this method for UIView:

extension UIView {
    func roundCorners(corners: UIRectCorner, radius: CGFloat) {
        let mask = CAShapeLayer()
        mask.frame = bounds
        mask.path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height:radius)).cgPath
        self.layer.mask = mask
    }
}

Usage:

star1.roundCorners(corners: [.topLeft, .bottomLeft], radius: 10)
star3.roundCorners(corners: [.topRight, .bottomRight], radius: 10)
alexburtnik
  • 7,661
  • 4
  • 32
  • 70
0

Try out this solution

let path2 = UIBezierPath(roundedRect:view1.bounds, byRoundingCorners:[.TopLeft, .BottomLeft, .TopRight, .BottomRight], cornerRadii: CGSizeMake(20, 20))
let maskLayer2 = CAShapeLayer()
maskLayer2.path = path2.CGPath
view2.layer.mask = maskLayer2

Hope this helps you out.

Md. Ibrahim Hassan
  • 5,359
  • 1
  • 25
  • 45
0

I put the images behind a view and set the background color to my liking. Then I got rid of any background color for my images. After that, I just rounded the corners of my view.

Faisal Syed
  • 921
  • 1
  • 11
  • 25