UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Strange TabBar Behaviour

Forums > SwiftUI

Hi,

I noticed a wired behaviour of the TabBar view and I'm curious if anybody else has made a similar experience or can add some knowledge.

Please consider the folowing simplified setup:

struct ContentView: View {
    var body: some View {
        TabView {
            DetailView()
                .tabItem {
                    Image(systemName: "star")
                    Text("Star")
                }
        }
    }
}

struct DetailView: View {
    var body: some View {
        Group{
            Text("Text A")
            Text("Text B")
        }
    }
}

Preview image

Instead of one tabitem for the DetailView this creates two tabbar items and each represents one of the Text views. This can be fixed replacing the Group with a VStack or HStack instead.

But consider the following scenario now, where teh inner views are displayed depending on some conditions.

truct DetailView: View {
    @State private let condition1 = false
    @State private let condition2 = false

    var body: some View {
        Group{
          if condition1 {
            Text("Text A")
          }

          if condition2 {
            Text("Text B")
          }
        }.onAppear {
          self.condition1 = self.checkAuthorization()
          self.condition2 = !self.condition1
        }
    }

    func checkAuthorization() -> Bool {
      // ...
      return false
    }
}

Now the tabbar stays empty. I assume this is because the group is empty initially (with both conditions set to false). But I thought that's what a Group is actually for?

Best regards Lenny

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.