SwiftUI에서 특정 모서리만 둥글게 만들기

June·2023년 2월 8일
2

SwiftUI-View

목록 보기
1/5
post-thumbnail

코드

import SwiftUI

extension View {
    func cornerRadius(_ radius: CGFloat, corners: UIRectCorner) -> some View {
        clipShape(RoundedCorner(radius: radius, corners: corners))
    }
}

struct RoundedCorner: Shape {
    var radius: CGFloat = .infinity
    var corners: UIRectCorner = .allCorners
    
    func path(in rect: CGRect) -> Path {
        let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        
        return Path(path.cgPath)
    }
}

사용하기

struct ContentView: View {
    var body: some View {
        Text("Hello, world!")
            .padding()
            .background(Color.red)
            .cornerRadius(30, corners: [.topLeft, .topRight, .bottomRight])
    }
}
profile
안다고 착각하지 말기

0개의 댓글