Rosalind
Rosalind

Reputation: 85

How to add a corner radius to a uiviewcontroller

I noticed that in snapchat they rounded their corners of a uiviewcontroller. I was wondering how could I replicate the same thing accross all view controllers. Would I declare it in the app delegate or in each individual view controller. Could someone please show me how to do it. I have an example image below. Look in the top corners of the view controller and you'll see that there rounded. I'm trying to replicate that.

example image

Upvotes: 4

Views: 7719

Answers (3)

Pixele9
Pixele9

Reputation: 442

This worked for me, hope it helps:

self.view.layer.cornerRadius = 10

Upvotes: 2

Frankie
Frankie

Reputation: 11918

You could use an extension, then you could apply a corner radius on any view, including the view controller's view. I haven't tried it though.

extension UIView {
    func makeCorner(withRadius radius: CGFloat) {
        self.layer.cornerRadius = radius
        self.layer.masksToBounds = true
        self.layer.isOpaque = false
    }
}

Usage:

myController.view.makeCorner(withRadius: 10.0)

Upvotes: 14

Pratik Patel
Pratik Patel

Reputation: 449

Firstly, that's a pretty bad idea. I would suggest that you look at the HIG documentation when designing your UI.

That being said, to implement something like this, you can create a base class which would inherit from UIViewController where you would set layer.cornerRadius of the view property. Then you can have all your view controllers inherit from your base class. You can do this in either viewDidLoad or viewWillAppear in your base class

Upvotes: -2

Related Questions