简体   繁体   中英

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.

示例图片

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)

This worked for me, hope it helps:

self.view.layer.cornerRadius = 10

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM