Replacing The Deprecated cornerRadius View Modifier In SwiftUI

Posted in SwiftUI

May 22nd, 2024

⏱ Reading Time: 2 mins

Welcome to a quick programming tip related to a deprecation that took place in SwiftUI. Up until iOS 17.4, the most direct and effortless way to round the corners of any view, giving it a softer and more appealing look, had been the cornerRadius(_:) view modifier. Using it had been a quick and straightforward task, as we just had to provide the desired radius value as argument. However, as of iOS 17.4, cornerRadius(_:) is no longer a valid option because it became deprecated, so here I’ll show you what to replace with.

For starters, take a look at the following small code example; it displays a color view with a specific frame, with the cornerRadius(_:) view modifier rounding its corners:

If you go and type “.cornerRadius” in Xcode you’ll get a warning regarding the deprecation of the view modifier. So, even though it’s been a “popular” modifier, it’s time to forget about it.

But how do we replace it?

The easiest and most convenient way to do that is to use the clipShape(_:)view modifier. It takes as argument a shape object and clips the target view using that shape. Among all built-in shapes that SwiftUI provides, the RoundedRectangle is the shape we have to use; it gets a radius value as argument, just like the cornerRadius(_:) view modifier does.

With that new information in mind, here’s how the previous code becomes, using the clipShape(_:) view modifier and the RoundedRectangle(cornerRadius:) shape this time:

Note that the clipShape(_:) modifier is a great alternative because it provides the ability to clip a view with any shape we want, both built-in and custom ones, and not just with the RoundedRectangle.

Now you know what to use instead of the cornerRadius(_:) view modifier in order to round corners in SwiftUI. Personally, I used to be using it a lot, but with the clipShape(_:) modifier around as well, its deprecation was a matter of time eventually.

Thank you for reading, enjoy your coding moments!

Stay Up To Date

Subscribe to my newsletter and get notifiied instantly when I post something new on SerialCoder.dev.

    We respect your privacy. Unsubscribe at any time.