'Round Specific Corners in a SwiftUI Mac App
I've been trying to figure out how to round specific corners of a SwiftUI View in a Mac app. All the solutions I can find (such as this) apply only to iOS since it has UIRectCorner. There is no NSRectCorner equivalent that I can find.
Back in the day, I would round a specific corner of an NSView like this:
layer?.cornerRadius = 5
layer?.maskedCorners = .layerMinXMinYCorner //Bottom-left corner
Has anyone found a way to round specific corners in a Mac app in SwiftUI?
Solution 1:[1]
Create a custom shape
import SwiftUI
struct RoundCorner: Shape {
// MARK: - PROPERTIES
var cornerRadius: CGFloat
var maskedCorners: UIRectCorner
// MARK: - PATH
func path(in rect: CGRect) -> Path {
let path = UIBezierPath(roundedRect: rect, byRoundingCorners: maskedCorners, cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
return Path(path.cgPath)
}
}
// MARK: - PREVIEW
struct RoundCorner_Previews: PreviewProvider {
static var previews: some View {
RoundCorner(cornerRadius: 20, maskedCorners: .allCorners)
}
}
And Apply this shape to your view
import SwiftUI
struct SwiftUIView: View {
// MARK: - BODY
var body: some View {
Text("Hello, World!")
.padding()
.background(
Color.orange
)
.clipShape(
RoundCorner(
cornerRadius: 12,
maskedCorners: [.topLeft, .bottomRight]
)//OUR CUSTOM SHAPE
)
}
}
// MARK: - PREVIEW
struct SwiftUIView_Previews: PreviewProvider {
static var previews: some View {
SwiftUIView()
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Umair Khan |

