【问题标题】:SwiftUI 2.0 disable window's zoom button on macOSSwiftUI 2.0 在 macOS 上禁用窗口的缩放按钮
【发布时间】:2021-02-03 02:58:39
【问题描述】:

我正在为 Mac 编写一个小应用程序..

我需要禁用(绿色按钮)才能全屏。

我使用的是 SwiftUI App 而不是 AppKit App Delegate

找不到如何为我的应用禁用全屏按钮。

有什么想法吗?

【问题讨论】:

  • 除非有更简单的方法,否则本文提供了一些在 SwiftUI 生命周期中访问窗口的重要技巧:lostmoa.com/blog/…
  • 嗯..检查...找不到解决方案。不过还是谢谢!

标签: macos swiftui


【解决方案1】:

因为没有人用更干净的 SwiftUI 唯一版本来回答:

struct ContentView: View {
    var body: some View {
        HostingWindowFinder { window in
            window?.standardWindowButton(.zoomButton)?.isHidden = true //this removes the green zoom button
        }
        Text("Hello world")
    }
}

struct HostingWindowFinder: NSViewRepresentable {
    var callback: (NSWindow?) -> ()

    func makeNSView(context: Self.Context) -> NSView {
        let view = NSView()
        DispatchQueue.main.async { [weak view] in
            self.callback(view?.window)
        }
        return view
    }
    func updateNSView(_ nsView: NSView, context: Context) {}
}

HostingWindowFinder 概念取自https://lostmoa.com/blog/ReadingTheCurrentWindowInANewSwiftUILifecycleApp/

【讨论】:

  • 哇...非常感谢大家的帮助..这很有效,非常感谢!
猜你喜欢
  • 1970-01-01
  • 2021-01-01
  • 2021-10-22
  • 1970-01-01
  • 1970-01-01
  • 2020-02-13
  • 1970-01-01
  • 2021-07-24
  • 1970-01-01
相关资源
最近更新 更多