Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disabled fullscreen button on MacCatalyst

UIWindow on macCatalyst with close, minimize and fullscreen buttons

Currently, I have this to stop resizing window:

#if targetEnvironment(macCatalyst)
windowScene.sizeRestrictions?.minimumSize = CGSize(width: 480, height: 900)
windowScene.sizeRestrictions?.maximumSize = CGSize(width: 480, height: 900)
#endif

let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()

but the fullscreen button makes it full screen anyway.

like image 364
Oleg Avatar asked Oct 28 '25 05:10

Oleg


1 Answers

Here is another approach that does not need Objective-C, selectors, or asynchronous calls. It also does not need target macros, iOS will simply skip if let NSApplication. Paste it into your view controller that appears first. Note that this disables the green full-screen button on all your windows. If you want to differentiate, use ideas from Asperi's Swift part.

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    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 = 3
            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])
        }
    }
}
like image 173
pommy Avatar answered Oct 29 '25 21:10

pommy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!