[英]Accessing NSWindow-like properties in Catalyst macOS app
我正在考慮將我的 macOS 應用程序移植到 Catalyst。
我的應用程序在所有其他應用程序 windows(包括碼頭)之上顯示了一個透明的 window(無標題欄,清晰的背景)。 為此,在我使用的非催化劑代碼中:
window.isOpaque = false
window.hasShadow = false
window.backgroundColor = .clear
window.styleMask = .borderless
window.isMovableByWindowBackground = true
window.level = .statusBar
使用 UIKit,到目前為止我只能刪除工具欄:
window.titleBar.titleVisibility
...但不知道其他設置。
我計划將來在 App Store 上提供該應用程序,但如果這樣做的唯一方法是使用私有 API 破解,那很好。
有任何想法嗎?
提前致謝
沒有官方的 API 可以做到這一點,但是您可以輕松訪問NSWindow
實例並直接對其進行修改。 您可以手動執行此操作,也可以使用Dynamic之類的庫(完全披露:我是作者):
let window = Dynamic.NSApplication.sharedApplication.delegate.hostWindowForUIWindow(uiWindow)
window.isOpaque = false
window.hasShadow = false
window.backgroundColor = Dynamic.NSColor.clearColor
window.styleMask = 0 /*borderless*/
window.isMovableByWindowBackground = true
window.level = 25 /*statusBar*/
通過從viewDidAppear()
調用 function ,我成功刪除了 Catalyst 上的關閉按鈕。 我稱它為AppDelegate().disableTitleBarButtons()
。 必須從視圖中確實出現。
AppDelegate().disableTitleBarButtons() 如下
func disableTitleBarButtons() {
func bitSet(_ bits: [Int]) -> UInt {
return bits.reduce(0) { $0 | (1 << $1) }
}
func property(_ property: String, object: NSObject, set: [Int], clear: [Int]) {
if let value = object.value(forKey: property) as? UInt {
object.setValue((value & ~bitSet(clear)) | bitSet(set), forKey: property)
}
}
// disable full-screen button
if let NSApplication = NSClassFromString("NSApplication") as? NSObject.Type,
let sharedApplication = NSApplication.value(forKeyPath: "sharedApplication") as? NSObject,
let windows = sharedApplication.value(forKeyPath: "windows") as? [NSObject]
{
for window in windows {
let resizable = 4
property("styleMask", object: window, set: [], clear: [resizable])
let fullScreenPrimary = 7
let fullScreenAuxiliary = 8
let fullScreenNone = 9
property("collectionBehavior", object: window, set: [fullScreenNone], clear: [fullScreenPrimary, fullScreenAuxiliary])
}
}
}
哪里說讓 resizable = 4,更改為 3 表示沒有最大化,更改為 2 表示沒有最小化,更改為 1 表示沒有關閉按鈕。
也可以使用其他數字或樣式掩碼設置。 祝你好運
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.