Home > AI > IOS > SwiftUI >

.toolbar

Example 1:

struct BottomToolbarView: View {
    var body: some View {
        NavigationView {
            Text("Hello, World!").padding()
                .navigationTitle("SwiftUI")
                .toolbar {
                    ToolbarItem(placement: .bottomBar) {
                        Button("Press Me") {
                            print("Pressed")
                        }
                    }
                }
        }
    }
}

Example 2: multiple bar items

struct BottomToolbarView: View {
    var body: some View {
        NavigationView {
            Text("Hello, World!").padding()
                .navigationTitle("SwiftUI")
                .toolbar {
                    ToolbarItem(placement: .bottomBar) {
                        HStack {
                            Button("First") {
                                print("Pressed")
                            }

                            Button("Second") {
                                print("Pressed")
                            }
                        }
                    }
                }
        }
    }
}

Another example for Type 2:

struct ContentView: View {
    
    var body: some View {
        NavigationView {
            VStack{
                Image("searchbar")
                Text("Test")
            }
            .toolbar {
                ToolbarItem(placement: .principal) {
                    HStack {
                        Text("9scoretrain")
                        Spacer()
                        NavigationLink(
                            destination: Text("SearchView"),
                            label: {
                                Image("searchbar")
                                    .resizable()
                                    .aspectRatio(contentMode: .fit)
                                    .frame(width:200)
                            })
                        Spacer()
                        NavigationLink(
                            destination: Text("CameraView"),
                            label: {
                                Image(systemName: "camera.viewfinder")
                            })
                    }
                }
            }
        }
    }
}

Leave a Reply