How to create top and bottom rounded corners for UIView with Swift

Darren
Programming with Swift
2 min readSep 11, 2018

Checkout my other posts over at programmingwithswift.com

By default if you set the corner radius on a views layer it will apply that radius to all the corners. If you only want some of the corners to have the radius applied, you will need to use maskedCorners.

In this quick tutorial I will show you how to use maskedCorners so that you can apply corner radius to specific corners of your view.

Below is the extension that I created:

Once you add that to your project all you need to do is the following in order to apply the corner radius to the corners you choose:

There are five options that you can pass through to the usingCorners argument. They are:

.topLeft
.topRight
.bottomLeft
.bottomRight
.allCorners

I hope that helped.

Some of my other articles:

How to blur and image using CIFilter with Swift: https://medium.com/programming-with-swift/how-to-blur-an-image-using-cifilter-with-swift-4-programming-with-swift-37b18fc35179

How to read a barcode or QRCode with Swift: https://medium.com/programming-with-swift/how-to-read-a-barcode-or-qrcode-with-swift-programming-with-swift-10d4315141d2

How to use SKStoreReviewController with Swift: https://medium.com/programming-with-swift/skstorereviewcontroller-tutorial-programming-with-swift-31a4faded01f

Getting Started with Realm Database with Swift: https://medium.com/programming-with-swift/getting-started-with-realm-database-for-ios-in-swift-4-4a246229e6a1

--

--