Navigation Bar Tutorial in iOS 18 – #30DaysOfSwift

In the third post of the #30DaysOfSwift series, I am going to teach you how to make a sticky navigation bar. Create a new SwiftView file called “HomeView” Replace the ContentView() with HomeView() in YourAppNameApp.swift (in this case, my project is called ShipiOS. Thus, file name is ShipiOSApp.Swift).


This content originally appeared on HackerNoon and was authored by Vaibhav

Day 2: Wandering around the multiple paths 🛣️

\ In the third post of the #30DaysOfSwift series, I am going to teach you how to make a sticky navigation bar.

\ The output would look something like this:

\n Screenshot from an app

Steps:

  • Create a new SwiftView file called "HomeView."

\

  • Replace the ContentView() with HomeView() in YourAppNameApp.swift (In this case, my project is called ShipiOS. Thus, file name is ShipiOSApp.swift). \n
var body: some Scene {
        WindowGroup {
            HomeView()
        }
    }
  • Lastly, add the following code in your HomeView() file: \n Image description

Code:

struct HomeView: View {
    @State private var selectedTab = 0 // Track the selected tab
    let generator = UIImpactFeedbackGenerator(style: .light) // For Haptics

    @EnvironmentObject var userCommonData: CommonData

    var body: some View {
        TabView(selection: $selectedTab) {
            ContentView() // Replace with your view
                .tabItem {
                    Label("Home", systemImage: "house")
                }
                .tag(0)

            SecondView() // Replace with your view
                .tabItem {
                    Label("Chats", systemImage: "tray")
                }
                .tag(1)

            ThirdView() // Replace with your view
                .tabItem {
                    Label("Profile", systemImage: "gearshape")
                }
                .tag(2)
        }
        .accentColor(Color("AppSecondaryColor"))
        .onChange(of: selectedTab) {
            generator.impactOccurred()
        }
    }

}

\ Does it work? Let me know :)

\ Happy coding!


This content originally appeared on HackerNoon and was authored by Vaibhav


Print Share Comment Cite Upload Translate Updates
APA

Vaibhav | Sciencx (2024-10-05T15:00:20+00:00) Navigation Bar Tutorial in iOS 18 – #30DaysOfSwift. Retrieved from https://www.scien.cx/2024/10/05/navigation-bar-tutorial-in-ios-18-30daysofswift/

MLA
" » Navigation Bar Tutorial in iOS 18 – #30DaysOfSwift." Vaibhav | Sciencx - Saturday October 5, 2024, https://www.scien.cx/2024/10/05/navigation-bar-tutorial-in-ios-18-30daysofswift/
HARVARD
Vaibhav | Sciencx Saturday October 5, 2024 » Navigation Bar Tutorial in iOS 18 – #30DaysOfSwift., viewed ,<https://www.scien.cx/2024/10/05/navigation-bar-tutorial-in-ios-18-30daysofswift/>
VANCOUVER
Vaibhav | Sciencx - » Navigation Bar Tutorial in iOS 18 – #30DaysOfSwift. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/05/navigation-bar-tutorial-in-ios-18-30daysofswift/
CHICAGO
" » Navigation Bar Tutorial in iOS 18 – #30DaysOfSwift." Vaibhav | Sciencx - Accessed . https://www.scien.cx/2024/10/05/navigation-bar-tutorial-in-ios-18-30daysofswift/
IEEE
" » Navigation Bar Tutorial in iOS 18 – #30DaysOfSwift." Vaibhav | Sciencx [Online]. Available: https://www.scien.cx/2024/10/05/navigation-bar-tutorial-in-ios-18-30daysofswift/. [Accessed: ]
rf:citation
» Navigation Bar Tutorial in iOS 18 – #30DaysOfSwift | Vaibhav | Sciencx | https://www.scien.cx/2024/10/05/navigation-bar-tutorial-in-ios-18-30daysofswift/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.