如何使用swift在macos上创建全屏覆盖?

问题描述 投票:0回答:1

有谁知道如何在不进入全屏模式的情况下使该视图覆盖全屏?本质上我只需要让这个窗口覆盖菜单栏所在的顶部区域。

我已经尝试过this SO帖子中的答案,但它对我不起作用。

struct CoverView: View {
    
    @EnvironmentObject private var appDelegate: AppDelegate
    
    @AppStorage(SettingsKeys.popUpMenuMessage) var popUpMenuMessage: String = "Time to give me twenty!"
    
    var body: some View {
        VStack {
            Text(popUpMenuMessage)
            
            Button(action: closeScreen) {
                Label("Close", systemImage: "arrow.up")
            }
        }
    }
    
    func closeScreen() {
        appDelegate.hideCoverWindow()
    }
}

struct BlurEffectView: NSViewRepresentable {
    func makeNSView(context: Context) -> NSVisualEffectView {
        // called when view is first created
        let view = NSVisualEffectView()

        view.blendingMode = .behindWindow
        view.state = .active
        view.material = .underWindowBackground

        return view
    }

    func updateNSView(_ nsView: NSVisualEffectView, context: Context) {}
}
@main
struct GiveMeTwentyApp: App {
    // Accessing App Delegate
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {        
        Window("CoverView", id: "CoverViewWindow", content: {
            GeometryReader { geometry in
                CoverView()
                    .frame(minWidth: geometry.size.width, minHeight: geometry.size.height)
                    .background(BlurEffectView().ignoresSafeArea())
                    .environmentObject(appDelegate)
            }
            
        }).windowStyle(.hiddenTitleBar) // hiding title bar itself
    }
}
class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
    var coverWindow: NSWindow?
    
    func applicationDidFinishLaunching(_ notification: Notification) {
        configureCoverWindow()
    }
    
    
    func configureCoverWindow() {
        coverWindow = NSApplication.shared.windows.first(where: { $0.title == "CoverView" })
        coverWindow?.standardWindowButton(.closeButton)?.isHidden = true
        coverWindow?.standardWindowButton(.miniaturizeButton)?.isHidden = true
        coverWindow?.standardWindowButton(.zoomButton)?.isHidden = true
        coverWindow?.level = .popUpMenu
    }
}

这就是 CoverView 目前的样子:

swift macos swiftui appkit
1个回答
0
投票

您可以在某处强制窗口全尺寸,如

onAppear

.onAppear {
    DispatchQueue.main.async {
        if let window = NSApplication.shared.windows.last {
            window.styleMask = .fullSizeContentView // 👈 Prevents window moving by user and covers the dock and menubar

            if let screen = window.screen ?? NSScreen.main {
                window.setFrame(screen.frame, display: true) // 👈 Use `visibleFrame` if you like to preserve the dock and menubar without letting the user move the window
            } else {
                window.zoom(self) // 👈 Fallback
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.