Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI: NavigationView inside TabView height not occupying the fullscreen

I seeing an unexpected UI issue in my SwiftUI project when I add a NavigationView inside a TabView.

Here's my code,


struct MainView: View {
    @State private var selectedTab: Int = 0

    var body: some View {
        TabView(selection: $selectedTab) {
            NavigationView {
                ScrollView {
                    VStack {
                        ForEach(0..<30) { i in
                            Text("Hello World")
                                .padding()
                                .frame(minWidth: .zero, maxWidth: .infinity)
                        }
                    }
                }.background(Color.red)
                .navigationTitle("Home View")
            }.background(Color.green)
            .navigationViewStyle(StackNavigationViewStyle())
            .tabItem {
                TabItem(title: "Explore", systemImage: selectedTab == 0 ? "house.fill" : "house")
            }.tag(1)
            
            ExploreView()
                .background(Color.red)
                .tabItem {
                    TabItem(title: "Explore", systemImage: selectedTab == 1 ? "safari.fill" : "safari")
                }.tag(1)
        }
    }
}

Here's my scene delegate code,


struct RootView: View {
    var body: some View {
        MainView()
    }
}

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo _: UISceneSession, options _: UIScene.ConnectionOptions) {
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            self.window = window
        }

        let host = UIHostingController(rootView: RootView())
        window?.rootViewController = host
        window?.makeKeyAndVisible()
    }
}

NavigationView-TabView-ScrollView

The height of the ScrollView(which resides inside the NavigationView) is not occupying the full screen. I tried all the tricks(padding, frame, GeometryReader, ...), but no luck. I couldn't figure out what am I doing wrong here, any help would be really appreciated.

like image 637
Karthik Avatar asked Oct 16 '25 12:10

Karthik


2 Answers

Thanks for the answers and for offering to help me.

I accidentally comment UITabBar's appearance logic, which fixed that problem. I'm not sure if this is a bug in SwiftUI or I'm missing something. Anyhow, commenting this following appearance fixed the issue,

// UITabBar.appearance().isTranslucent = false   <<-- Remove this
like image 95
Karthik Avatar answered Oct 19 '25 02:10

Karthik


It is inside safe area, you need to ignore it

}.background(Color.red)
.navigationTitle("Home View")
.ignoresSafeArea()             // << here !!
like image 30
Asperi Avatar answered Oct 19 '25 00:10

Asperi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!