Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI - Can I have a tab bar show on iPhone and sidebar show on iPad? [closed]

Tags:

ios

swift

swiftui

I Am developing a SwiftUI App for iOS that runs on iPhone and iPad. I am wondering if it is possible to show a tab bar on iPhone, but show a vertical side bar on iPad to make better use of the large screen. The tab bar and the side bar would have the same functionality, switching between tabs. It could look something like this Image I found on twitter except I don't need to have a macOs version of it. Could anyone help me out?

like image 783
ryandu Avatar asked Mar 02 '23 06:03

ryandu


2 Answers

You need to add conditional check, somthing like folling:

struct TabSideBar: View {

    #if os(iOS)
    @Environment(\.horizontalSizeClass) private var horizontalSizeClass
    #endif
    
    @ViewBuilder var body: some View {
        #if os(iOS)
        if horizontalSizeClass == .compact {
            TabBarContentView()
        } else {
            SideBarContentView()
        }
        #else //MacOSView
            SideBarContentView()
        #endif
    }
    
}
like image 164
Prafulla Avatar answered Mar 04 '23 21:03

Prafulla


Here is possible approach to have explicitly separated views for different devices

struct ContentView: View {
    var body: some View {
        if UIDevice.current.userInterfaceIdiom == .pad {
            YourSidebarView()
        } else {
            YourTabView()
        }
    }
}
like image 24
Asperi Avatar answered Mar 04 '23 21:03

Asperi