zainhoda / swiftui.gallery

A gallery of SwiftUI code example snippets and their resulting views
https://swiftui.gallery
MIT License
47 stars 5 forks source link

TabBar View #1

Open pavan288 opened 5 years ago

pavan288 commented 5 years ago

This is an example of how to create a replica of the classic UITabBarViewController from Swift in SwiftUI.

Screenshot 2019-10-07 at 2 39 10 PM Screenshot 2019-10-07 at 2 39 07 PM
import SwiftUI

struct TabBarView: View {
    var body: some View {
        TabView {
            Tab1View()
                .tabItem {
                    Image(systemName: "list.dash")
                    Text("Tab 1")
            }

            Tab2View()
                .tabItem {
                        Image(systemName: "square.and.pencil")
                        Text("Tab 2")
                }
        }
    }
}

Change the method in the SceneDelegate.swift file to this:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

        // Create the SwiftUI view that provides the window contents.
        let contentView = TabBarView()

        // Use a UIHostingController as window root view controller.
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }
import SwiftUI

struct Tab1View: View {
    var body: some View {
        Text("This is the first tab")
    }
}
import SwiftUI

struct Tab2View: View {
    var body: some View {
        Text("This is the second tab")
    }
}