SwiftUI - Catalyst translucent sidebar
Asked Answered
G

3

6

Goal is to make a translucent sidebar on Mac Catalyst.

The code bellow gives a not translucent sidebar (image 1).

On Mac (not catalyst) the sidebar looks fine (image 2).

is it possible to have a translucent sidebar on Mac Catalyst?

enter image description here

enter image description here

import SwiftUI

struct ContentView: View {
    var body: some View {
        
        NavigationView {
            
            //sidebar
            List {
                Label("Books", systemImage: "book.closed")
                Label("Tutorials", systemImage: "list.bullet.rectangle")
         
            }
            .background(Color.clear)

            .listStyle(SidebarListStyle())
            
            //content
            Text("Sidebar")
            .navigationTitle("Sidebar")
        }
        
        
    }
}
Grizel answered 11/1, 2021 at 19:12 Comment(6)
Any news about that? I have the same issue. – Rexrexana
nope, seems only UIKit can do it, as apple documentation... developer.apple.com/documentation/uikit/mac_catalyst/… – Grizel
did you find anything? – Grizel
yep, just UIKit πŸ˜” – Fasto
what about adding background blur modifier? I tried but didn't work... – Grizel
You need to wrap you views in a UISplitViewController. See https://mcmap.net/q/1778950/-swiftui-amp-mac-catalyst-sidebar-is-not-displayed-correctly – Bougie
G
4

You should select "Optimize Interface for Mac" in your target's General settings tab. Then the sidebar will be translucent.

Glede answered 11/1, 2022 at 6:13 Comment(1)
This solved it for me, thanks! – Soundboard
A
1

Start with the AppDelegate main and follow Apple's tutorial re: UISplitViewController "Apply a Translucent Background to Your Primary View Controller".

https://developer.apple.com/documentation/uikit/mac_catalyst/optimizing_your_ipad_app_for_mac

In wrapping UISplitViewController in a UIViewControllerRepresentable, I wasn't able to get translucency, but did get full-height sidebar.

Animadvert answered 9/5, 2021 at 1:9 Comment(0)
U
0

I figured out that using .background(Color.clear) on sidebar View makes possible translucent background even if ListStyle is not specified as SidebarListStyle(). Works in Xcode 13.1 for me

struct ContentView: View {
    var body: some View {
        NavigationView { // without wrapping to NavigationView it won't work
            List { // can be VStack or HStack
                Text("Hello, world!")
                  .padding()
            }
            .listStyle(SidebarListStyle()) // works with other styles
        Text("")
    }
  }
}

struct YourApp: App {
   var body: some Scene {
      WindowGroup {
        ContentView()
            .toolbar {
                Button {
                    
                } label: {
                    Image(systemName: "gear")
                }

            }
            .background(Color.clear) // 3 <-- MUST HAVE!
      }
   }
}
Ulcerate answered 20/11, 2021 at 13:25 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.