🔰Presentations

👉 SwiftUI Tutorials ⟩ Working with UI ControlsDisplay a User Profile - Step 8.

// Toolbar & Modal Sheet
struct CategoryHome: View {
    
    @EnvironmentObject var modelData: ModelData
    
    /// ⭐️ 1. control the presentation of the `ProfileHost` view
    @State private var showingProfile = false
    
    var body: some View {
        NavigationView {
            List { /* list items ... */ }
                .navigationTitle("Featured")
                
                /// ⭐️ 2. add a button to the navigation bar
                .toolbar {
                    Button { showingProfile.toggle() } label: {
                        Label("User Profile", systemImage: "person.crop.circle")
                    }
                }
                
                /// ⭐️ 3. present the `ProfileHost` view when the user taps the button.
                .sheet(isPresented: $showingProfile) {
                    ProfileHost().environmentObject(modelData)
                }
        }
    }
}

Last updated

Was this helpful?