.roundedBorder()

SwiftUI โŸฉ Views โŸฉ View Modifiers โŸฉ examples โŸฉ

import SwiftUI

// custom view modifier
struct RoundedBorderStyle: ViewModifier {
    
    // init variables
    var cornerRadius = 30.0
    
    // โญ๏ธ .dark / .light mode
    @Environment(\.colorScheme) var colorScheme     
    
    // ViewModifier protocol requirement
    func body(content: Content) -> some View {
        content.overlay(
            RoundedRectangle(
                cornerRadius: cornerRadius, 
                style: .continuous
            ).stroke(.linearGradient(
                colors: [
                    .white.opacity(colorScheme == .dark ? 0.6 : 0.3),
                    .black.opacity(colorScheme == .dark ? 0.3 : 0.1)
                ],
                startPoint: .top,
                endPoint  : .bottom
            ))
            .blendMode(.overlay)
        )
    }
}

extension View {
    public func roundedBorder(cornerRadius: Double = 30) -> some View {
        modifier(RoundedBorderStyle(cornerRadius: cornerRadius))
    }
}

Last updated