SwiftUI: How to make a hidden UITabBar display correctly on View appearing?

Viewed 1199

I have a TabView, with one of the tabs being a NavigationView. I want the tab bar to be hidden on the navigation destination view. I have achieved this, but the view only appears properly after the first rotation. How do I get it to appear properly the first time (2nd image)?

struct ContentView: View {
    var rowIndexes : [Int] = [0,1,2,3,4,5,6]
    var body: some View {
        TabView {

            NavigationView {
                List {
                    ForEach(self.rowIndexes, id: \.self) {i in
                        NavigationLink(
                        destination: Color(.blue)
                            .onAppear(perform: {
                                Global.tabBar!.isHidden = true
                            })
                            .onDisappear(perform: {
                                Global.tabBar!.isHidden = false
                            })
                        ) {
                            Text("\(i)")
                        }
                    }
                }
            }.tabItem {
                    Image(systemName: "list.number")
                    Text("List View")
            }

            NavigationView {
                Text("Options View")
            }.tabItem {
                    Image(systemName: "wrench")
                    Text("Options")
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
struct Global {
    static var tabBar : UITabBar?
}
extension UITabBar {
    override open func didMoveToSuperview() {
        super.didMoveToSuperview()
        Global.tabBar = self
        print("Tab Bar moved to superview")
    }
}

Here's what the screen looks like after clicking on a link in the list for the first time (INCORRECT, with gap at the bottom where the tab bar would be if it wasn't hidden):

enter image description here

Here's what the screen looks like after rotating it to landscape, then back to portrait (CORRECT, blue View extending all the way to the bottom):

enter image description here

Is there a way to force the redraw, or simulate a rotation and back? I have tried various @State, @EnvironmentObect, and @ObservedObject solutions, but none work.

1 Answers

Adding ignore bottom safe area gives behaviour as you want.

NavigationLink(
    destination: Color(.blue).edgesIgnoringSafeArea(.bottom)  // << here !!
    .onAppear(perform: {
        MyGlobal.tabBar!.isHidden = true

Tested with Xcode 11.4 / iOS 13.4.

Related