article

Monday, March 7, 2022

SwiftUI Simple Custome Modifier

SwiftUI Simple Custome Modifier

ContentView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
//  ContentView.swift
//  SwiftUIProject
//
//  Created by Cairocoders
//
 
import SwiftUI
 
extension View {
    func addNavigationView(title: String) -> some View {
        NavigationView {
            self
                .navigationTitle(title)
        }
    }
     
    func centerHorizontal() -> some View {
        HStack {
            Spacer()
            self
            Spacer()
        }
    }
     
    func customfont() -> some View {
        self
            .font(.system(size: 20, weight: .bold, design: .monospaced))
            .foregroundColor(.indigo)
    }
}
 
struct ContentView: View {
    var body: some View {
        Form {
            Text("Hello, world!")
                .customfont()
                .centerHorizontal()
             
            Button("Submit") {
                print("Button pressed.")
            }
            .centerHorizontal()
        }
        .addNavigationView(title: "Custom Modifier")
    }
}
 
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Related Post