【问题标题】:How can I hide statusBar of a View/Window in macOS in SwiftUI?如何在 SwiftUI 中隐藏 macOS 中视图/窗口的状态栏?
【发布时间】:2020-11-22 02:30:45
【问题描述】:

我试图隐藏视图的 statusBar,但我注意到它在 macOS 中不起作用,Blur 也不起作用 opacity不工作!看起来我正在尝试编程新语言,苹果说代码到位并应用到任何地方!为什么我不能在 macOS 中完成这些简单的任务?

struct ContentView: View {
    var body: some View {        
        HStack {
            Button { print("OK was clicked!") } label: { Text("OK").frame(width: 100) }
            Button { print("Close was clicked!") } label: { Text("Close").frame(width: 100) }
        }
        .frame(width: 400, height: 200)
        .background(Color.white.opacity(0.4).blur(radius: 0.5))
        //.statusBar(hidden: true)
    }
}

import SwiftUI

@main
struct macOSApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

【问题讨论】:

标签: macos swiftui


【解决方案1】:

希望对您有所帮助。
谢谢。

class AppDelegate: NSObject, UIApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {

    // Create the content view, to display the contents
    let contentView = ContentView()
    //to display content view beyond status bar.
        .edgesIgnoringSafeArea(.top)  

    // Now, just create the window and set the content view. 
    window = NSWindow(
        contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
        styleMask: [.titled, .closable, .miniaturizable, .texturedBackground, .resizable, .fullSizeContentView],
        backing: .buffered, defer: false)
    window.center()
    window.setFrameAutosaveName("Main Window")
    
    //So, this does the maigc
    //Hides the titlebar.
    window.titlebarAppearsTransparent = true 
    window.titleVisibility = .hidden        

    window.contentView = NSHostingView(rootView: contentView)
    window.makeKeyAndOrderFront(nil)
}
}

【讨论】:

  • 我的项目中没有看到App Delegate,我做了截图
  • 在你的 macOsApp.swift 中。您可以在 macosApp 结构中的以下行 @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate 并在该文件中的某处添加复制并粘贴已编辑的答案。
  • 你能解释更多吗,我不能让它工作或者也许是一个例子
猜你喜欢
  • 2019-11-15
  • 1970-01-01
  • 2014-06-06
  • 1970-01-01
  • 2020-09-10
  • 2019-10-22
  • 2014-02-08
  • 1970-01-01
相关资源
最近更新 更多