ios - SwiftUI TabView hidden tabbar navigation stack animation order issue - Stack Overflow

I am having the following view:import SwiftUIstruct HiddenTabs: View {var body: some View {TabView {Nav

I am having the following view:


import SwiftUI

struct HiddenTabs: View {
    var body: some View {
        TabView {
            NavigationStack {
                NavigationLink("Tap Me") {
                    Text("Detail View")
                        .toolbar(.hidden, for: .tabBar)
                }
                .padding()
                .background(.secondary)
                .navigationTitle("Primary View")
            }
            .tabItem {
                Label("Home", systemImage: "house")
            }
        }.background(.purple)
    }
}

#Preview {
    HiddenTabs()
}

It works fine, but the tabs appearance after the back button press on navigation view is too late and causes the whole content of the tab to jump up.

Is it possible to make tabs appearance don't cause content jump? Ideally, I don't want any animation besides NavigationStack "< Back".

I am having the following view:


import SwiftUI

struct HiddenTabs: View {
    var body: some View {
        TabView {
            NavigationStack {
                NavigationLink("Tap Me") {
                    Text("Detail View")
                        .toolbar(.hidden, for: .tabBar)
                }
                .padding()
                .background(.secondary)
                .navigationTitle("Primary View")
            }
            .tabItem {
                Label("Home", systemImage: "house")
            }
        }.background(.purple)
    }
}

#Preview {
    HiddenTabs()
}

It works fine, but the tabs appearance after the back button press on navigation view is too late and causes the whole content of the tab to jump up.

Is it possible to make tabs appearance don't cause content jump? Ideally, I don't want any animation besides NavigationStack "< Back".

Share Improve this question edited Mar 23 at 12:05 Bogdan Gusiev asked Mar 23 at 10:01 Bogdan GusievBogdan Gusiev 8,31716 gold badges62 silver badges83 bronze badges 2
  • See How do I animate hiding and showing tab bar in SwiftUI? – Benzy Neez Commented Mar 23 at 10:06
  • @BenzyNeez what if I don't want to animate it? I see other apps behaving with neither animation nor content jump (ex telegram). The solutions proposed in that question are providing better animation, but I don't I really need anything beyond NavigationStack animation. – Bogdan Gusiev Commented Mar 23 at 11:47
Add a comment  | 

1 Answer 1

Reset to default 1

The post How do I animate hiding and showing tab bar in SwiftUI? explores ways of animating the change in tabbar visibility.

You said in a comment, that you don't need animation. In which case, it may be sufficient to use a state variable to determine the visibility.

I found it works best to update the state variable in separate .onAppear callbacks for the NavigationLink and its destination. If instead you try to reset it in .onDisappear for the destination, there is still a jump.

@State private var toolbarVisibility = Visibility.visible
NavigationStack {
    NavigationLink("Tap Me") {
        Text("Detail View")
            .onAppear { toolbarVisibility = .hidden }
    }
    .padding()
    .background(.secondary)
    .navigationTitle("Primary View")
    .onAppear { toolbarVisibility = .visible }
}
.toolbar(toolbarVisibility, for: .tabBar)
.tabItem {
    Label("Home", systemImage: "house")
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744290833a4567022.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信