SwiftUI Custom View Modifiers

2 minute read Published: 2020-11-27

SwiftUI에서 ViewModifier 를 이용하여 Custom View를 만드는 방법

SwiftUI Custom View Modifiers

Create Custom Button

viewModifier1
struct CustomButton: View {
    var body: some View {
        Button(action: {
            print("select button")
        }, label: {
            Text("Continue")
                .font(.system(size: 16))
                .foregroundColor(.white)
                .padding(.horizontal, 14)
                .padding(.vertical, 10)
                .background(Color.blue)
                .overlay(
                    RoundedRectangle(cornerRadius: 3)
                        .strokeBorder(style: StrokeStyle(lineWidth: 1))
                        .foregroundColor(Color(.sRGB, red: 0.1, green: 0.1, blue: 0.1, opacity: 1))
                        .cornerRadius(4)
                        .shadow(color: Color(.sRGB, red: 0, green: 0, blue: 0, opacity: 0.5), radius: 5, x: 0, y: 0)
                )
        })
    }
}

Using Modifier

struct CustomModifierButton: View {
    var body: some View {
        Button(action: {
            print("select button")
        }, label: {
            Text("Cancel")
                .modifier(ButtonModifier())
        })
    }
}


struct ButtonModifier: ViewModifier {
    func body(content: Content) -> some View {
        return content
            .font(.system(size: 16))
            .foregroundColor(.white)
            .padding(.horizontal, 14)
            .padding(.vertical, 10)
            .background(Color.green)
            .overlay(
                RoundedRectangle(cornerRadius: 3)
                    .strokeBorder(style: StrokeStyle(lineWidth: 1))
                    .foregroundColor(Color(.sRGB, red: 0.1, green: 0.1, blue: 0.1, opacity: 1))
                    .cornerRadius(4)
                    .shadow(color: Color(.sRGB, red: 0, green: 0, blue: 0, opacity: 0.5), radius: 5, x: 0, y: 0)
            )
    }
}

Using Modifier added state parameter

viewModifier2
struct ButtonModifier: ViewModifier {
    @State var backgroundColor = Color.red
	...
struct ModifierStackView: View {
    var body: some View {
        HStack {
            CustomBlueButton()
            CustomRedButton()
            Button(action: {
                print("select button")
            }, label: {
                Text("Cancel")
                    .modifier(ButtonModifier(backgroundColor: .green))
            })
        }
    }
}

struct CustomBlueButton: View {
    var body: some View {
        Button(action: {
            print("select button")
        }, label: {
            Text("Continue")
                .modifier(ButtonModifier(backgroundColor: .blue))
        })
    }
}

struct CustomRedButton: View {
    var body: some View {
        Button(action: {
            print("select button")
        }, label: {
            Text("Okay")
                .modifier(ButtonModifier(backgroundColor: .red))
        })
    }
}

struct ButtonModifier: ViewModifier {
    @State var backgroundColor = Color.red
    
    func body(content: Content) -> some View {
        return content
            .font(.system(size: 16))
            .foregroundColor(.white)
            .padding(.horizontal, 14)
            .padding(.vertical, 10)
            .background(backgroundColor)
            .overlay(
                RoundedRectangle(cornerRadius: 3)
                    .strokeBorder(style: StrokeStyle(lineWidth: 1))
                    .foregroundColor(Color(.sRGB, red: 0.1, green: 0.1, blue: 0.1, opacity: 1))
                    .cornerRadius(4)
                    .shadow(color: Color(.sRGB, red: 0, green: 0, blue: 0, opacity: 0.5), radius: 5, x: 0, y: 0)
            )
    }
}