Home > Blockchain >  Updating windowStyle of WindowGroup in macOS SwiftUI-life cycle
Updating windowStyle of WindowGroup in macOS SwiftUI-life cycle

Time:01-07

I want be able to update windowStyle of my App via ContentView, I tried 2 approach, but both failed! Looking to solve the issue with macOS SwiftUI-life cycle.

@main
struct macOSApp: App {

    @State private var windowToolbarStyle: Bool = false

    var body: some Scene {
        
        WindowGroup {
            ContentView(windowToolbarStyle: $windowToolbarStyle)
        }
        .windowStyle(windowToolbarStyle ? TitleBarWindowStyle() : HiddenTitleBarWindowStyle())

    }
}

@main
struct macOSApp: App {

    @State private var windowToolbarStyle: Bool = false

    var body: some Scene {
        
        if windowToolbarStyle {
            WindowGroup {
                ContentView(windowToolbarStyle: $windowToolbarStyle)
            }
        }
        else {
            WindowGroup {
                ContentView(windowToolbarStyle: $windowToolbarStyle)
            }
            .windowStyle(HiddenTitleBarWindowStyle())
        }
    }
}

CodePudding user response:

This isn't currently possible using the SwiftUI app lifecycle. SceneBuilder doesn't implement buildEither or buildIf methods, which are required to support conditionals. Compare to ViewBuilder, which does implement them and supports conditionals.

  •  Tags:  
  • Related