Replacing The Deprecated cornerRadius View Modifier In SwiftUI
It’s okay to deprecate APIs when there are better tools available
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:
Color.indigo
.frame(width: 250, height: 200)
.cornerRadius(24)
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…