Skip to content

It seems the page will be recreated when switch between tabs each time. #7

@Horse888

Description

@Horse888
struct ContentView: View {
    
    private enum Item: Int, Tabbable {
        case first = 0
        case second
        case third
        
        var icon: String {
            switch self {
                case .first: return "house"
                case .second: return "magnifyingglass"
                case .third: return "person"
            }
        }
        
        var title: String {
            switch self {
                case .first: return "First"
                case .second: return "Second"
                case .third: return "Third"
            }
        }
    }
    
    @State private var selection: Item = .first
    @State private var visibility: TabBarVisibility = .visible
    
    var body: some View {
        TabBar(selection: $selection, visibility: $visibility) {
            Button {
                withAnimation {
                    visibility.toggle()
                }
            } label: {
                Text("Hide/Show TabBar")
            }
            .tabItem(for: Item.first)
            
			Second()
                .tabItem(for: Item.second)
            
            Text("Third")
                .tabItem(for: Item.third)
        }
        .tabBar(style: CustomTabBarStyle())
        .tabItem(style: CustomTabItemStyle())
    }
}

struct ContentSystemTabView: View {
	var body: some View {
		TabView {
			Text("First")
				.tabItem {
					Text("first")
				}

			Second()
				.tabItem {
					Text("second")
				}
		}
	}
}

struct Second: View {
	init() {
		print("init...")
	}
	var body: some View {
		Text("Second")
	}
}

You will find the difference between TabView.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions