2

I'm using Introspect to hide the tab bar on child navigation link pages. However, I've noticed some odd behavior when the app is backgrounded and then brought back to the foreground.

It seems like initially, the hidden tab bar is still taking up some space, but this disappears when cycling the app back to the foreground. I'm not sure if this is SwiftUI behavior or has to do with how I'm using Introspect / UIKit.

It's causing layout issues in my app, so I'd like to make the spacing consistent if possible.

View spacing changes

Here's a minimal example that shows the behavior:

import SwiftUI
import Introspect

struct ContentView: View {
    var body: some View {
        TabView {
            VStack {
                Spacer()
                Text("Hello, world!")
            }
        }
        .border(Color.red)
        .introspectTabBarController { tabBarController in
            tabBarController.tabBar.isHidden = true
        }
    }
}
sia
  • 256
  • 3
  • 14

1 Answers1

1

Here is the late answer. Basically add tabbar height to current view frame. And onDissappear restore view frame size

import SwiftUI
import Introspect
@State var uiTabarController: UITabBarController?
@State var tabBarFrame: CGRect?
struct ContentView: View {
    var body: some View {
        TabView {
            VStack {
                Spacer()
                Text("Hello, world!")
            }
        }
        .border(Color.red)
        .introspectTabBarController { (UITabBarController) in
            uiTabarController = UITabBarController
            self.tabBarFrame = uiTabarController?.view.frame
            uiTabarController?.tabBar.isHidden = true
            uiTabarController?.view.frame = CGRect(x:0, y:0, width:tabBarFrame!.width, height:tabBarFrame!.height+UITabBarController.tabBar.frame.height);
        }
        .onDisappear {
            if let frame = self.tabBarFrame {
                self.uiTabarController?.tabBar.isHidden = false
                uiTabarController?.view.frame = frame
            }
            
        }
    }
}
Emre
  • 103
  • 2
  • 11