Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Horse888 opened this issue Jun 2, 2022 · 7 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@Horse888
Copy link

Horse888 commented Jun 2, 2022

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.

@onl1ner onl1ner added enhancement New feature or request help wanted Extra attention is needed labels Jun 2, 2022
@onl1ner
Copy link
Owner

onl1ner commented Jun 2, 2022

Hello, @Horse888!

Yeah, I know about this issue and unfortunately have no idea how to fix that currently. Apple's TabView most-likely a wrapper around UITabBar, so it has this feature that saves the state of the child views, but as this library purely written in SwiftUI it has this downside.

I've added to this issue help wanted Extra attention is needed tag and if you have any idea how to fix that feel free to share it with me or open PR. Thanks!

@davoxdavo
Copy link

What if you will create lazy instances of each TabView than just. reuse them

@onl1ner
Copy link
Owner

onl1ner commented Jan 22, 2023

Hello, @davoxdavo!

Sorry for late response, could you please clarify your idea, because I think I do not understand your suggestion well

@nashschultz
Copy link

I've seen it done by changing the opacity to 1.0 or 0 based on the selected view rather than reloading it. I'm not sure how that affects performance

@jcbriones
Copy link

jcbriones commented Mar 28, 2023

Opacity won't work unless you want to do a fade in fade out effect. Basically hiding it with hidden modifier, disable the view, and disabling accessibility to the view that way it user cannot interact with while its hidden.

@Jinya
Copy link

Jinya commented Jun 27, 2023

I think this approach UIKitTabView would be what you guys are looking for.

@winddpan
Copy link

I did some tests and it seems to be fine

#24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

7 participants