Porting a game to macOS Catalyst, but window is quite small. Is it possible to start in full screen instead?
Yes it is possible to start in full screen.
To switch to full screen you need to use AppKit and NSApplication class but currently that is not available in Mac Catalyst app directly. However you can access it from another plugin bundle. This is how you do that and switch to full screen on app start:
Step 1. You need to create a new mac bundle target in your app. Click File -> New -> Target -> macOS -> Bundle and then click button Next. Enter product name for example MacBundle and click button Finish.
Step 2. Select the newly created group MacBundle in your project and click File -> New -> macOS -> Cocoa Class and the click button Next. Enter class name for example MacApp that is a subclass of NSObject and set language to Objective-C. Click Next, make sure that MacBundle target is selected and click button Create.
Step 3. Edit MacApp.h like this:
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface MacApp : NSObject
+ (void)toggleFullScreen;
@end
NS_ASSUME_NONNULL_END
Step 4. Edit MacApp.m like this:
#import "MacApp.h"
@implementation MacApp
+ (void)toggleFullScreen {
[[[[NSApplication sharedApplication] windows] firstObject] toggleFullScreen:nil];
}
@end
Step 5. Click on your project and in Targets section select your main app target (the same which is for iOS)
Step 6. In General tab scroll down to Frameworks, Libraries, and Embeeded Content section and click + button. In new popup for choosing framework select MacBundle.bundle and click button Add to embeed this bundle in your main app.
Step 7. Now you can call toggleFullScreen method from your MacApp class that is in MacBundle from your main iOS code. To make it work you can call it once from viewDidAppear from the first UIViewController that appears in your app. You can call it like below:
static var needsFullScreen = true
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if Self.needsFullScreen {
Bundle(path: Bundle.main.builtInPlugInsPath?.appending("/MacBundle.bundle") ?? "")?.load()
let macApp = NSClassFromString("MacApp") as? NSObjectProtocol
macApp?.perform(NSSelectorFromString("toggleFullScreen"))
Self.needsFullScreen = false
}
}
Alternatively you could create a protocol with that toggleFullScreen method.
After that when you launch the app it will switch to fullscreen automatically.
If you do not plan to use other AppKit stuff then for this one toggleFullScreen call shown in previous method you can just call it without plugin bundle with runtime functions once from viewDidAppear from the first UIViewController that appears in your app like below:
static var needsFullScreen = true
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if Self.needsFullScreen {
(NSClassFromString("NSApplication")?.value(forKeyPath: "sharedApplication.windows") as? [AnyObject])?.first?.perform(Selector("toggleFullScreen:"))
Self.needsFullScreen = false
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With