1

I am trying to set the corner radius of a UIView using UIBezierPath. I created the following extension for this:

extension UIView
{    
    func roundedView(usingCorners corners: UIRectCorner, cornerRadii: CGSize)
    {
        let path = UIBezierPath(roundedRect: self.bounds,
                                byRoundingCorners: corners,
                                cornerRadii: cornerRadii)

        let maskLayer = CAShapeLayer()
        maskLayer.path = path.cgPath

        self.layer.mask = maskLayer
    }
}


cvTutorialContainerView.roundedView(usingCorners: [.topRight, .bottomRight, .topLeft, .bottomLeft],
                                         cornerRadii: CGSize(width: 8, height: 8))

The function is being called from viewDidLayoutSubviews():

cvTutorialContainerView.roundedView(usingCorners: [.topRight, .bottomRight, .topLeft, .bottomLeft],
                                         cornerRadii: CGSize(width: 8, height: 8))

The problem is I only get a radius for the top corners, for some reasons but only on some occasions, like when using a UIPageView or an UIScrollView.

I tried cvTutorialContainerView.layer.cornerRadius = 8.0 but it didn't work. I also tried calling the function from viewDidLoad and viewDidAppear but again, no luck.

Any ideas?

daydr3am3r
  • 920
  • 4
  • 12
  • 31

1 Answers1

2

Why draw a bezierPath and apply mask while you have something that does that already?

yourView.layer.masksToBounds = true // You seem to be missing this part

If the above property is not specified, your subviews won't get clipped to yourView's layer and that's probably why the below line didn't work for you initially

yourView.layer.cornerRadius = 8 //your radius

P.S. You can also round specific-corners using:

yourView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner] //This will round the top-left and top-right corners
Lokesh SN
  • 1,583
  • 7
  • 23
  • Thanks for the suggestion. As I mentioned above, I tried it already and unfortunately it doesn't work. Also, I am using clipsToBounds = true and layer.cornerRadius everywhere else in my app. The only 2 situations I am having issues with this approach are the one above (UIView and PageViewController) and for a UIView inside a UIScrollView. – daydr3am3r Jul 30 '19 at 12:34
  • Oh, but this code is supposed to work irrespective of the scenarios you mentioned. I myself have a project where I use rounded views inside scrollView and it works fine for me. Would be cool if you can provide more detail about the scenarios you're talking about (the code, I mean) – Lokesh SN Jul 30 '19 at 12:42
  • I have the following ViewControllers in this scenario: https://i.imgur.com/NkJmnwM.png The view I am trying to apply the roundedCorners to (cvTutorialContainerView) is a UIContainerView, the grey one above the Start button. The code I am using, including your suggestion, is this: https://pastebin.com/0Uy47gaj – daydr3am3r Jul 30 '19 at 12:55
  • Hmm, the code seems fine. Perhaps the place where you set the `cornerRadius` is not right but it still doesn't explain how it works for the other views. From your image, I can see that the `cvTutorialContainerView` is defined through storyboard, can you try to set the radius there like this?: https://stackoverflow.com/a/34215492/9293498 – Lokesh SN Jul 31 '19 at 06:04
  • Thanks. Just tried it and it still does not work. The thing is it used to work. Initially I had an extension with predefined values. I modified the function to accept parameters and it stoped working. I changed the function back but it remained the same. And as I mentioned above, it didn't work for the views inside a UIScrollView (I changed the design on that part and I no longer need to have rounded corners there but I still found it strange). Oh, btw, I don't have Clip Subviews in Xcode, I have Clip to Bounds. – daydr3am3r Jul 31 '19 at 06:45
  • Oh, and I checked the height of the ContainerView and the attached CVs. I thought that maybe it's higher than the visible area and I simply don't see the rounded corners. Not sure if this makes any sense but it was the last thing I could think of. – daydr3am3r Jul 31 '19 at 06:47
  • Hmm, I get it. And yea, that's strange. This is probably one of those issues that require more than just snippets of code to solve – Lokesh SN Jul 31 '19 at 07:16