问题
Porting a game to Mac Catalyst - but window is quite small. Is it possible to start in full screen?
回答1:
There's no simple setting that says "start full screen". But you can set the window's frame on startup.
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
#if targetEnvironment(macCatalyst)
window?.frame = CGRect(origin: .zero, size: CGSize(width: 1600, height: 1000))
#endif
Obviously that's not ideal because you don't want to hardcode a specific size.
You could get the screen's size as follows. But in my own tests the returned value is not accurate. This might be a bug in Mac Catalyst.
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let winScene = (scene as? UIWindowScene) else { return }
#if targetEnvironment(macCatalyst)
let screen = winScene.screen
let size = screen.nativeBounds.size
window?.frame = CGRect(origin: .zero, size: size)
#endif
}
This makes it bigger but it's not truly fullscreen because, at least in my tests, the returned screen size doesn't actually match the size of the screen.
But this should give you some ideas.
You can also set a minimum and maximum size on your screen:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let winScene = (scene as? UIWindowScene) else { return }
#if targetEnvironment(macCatalyst)
if let sizes = winScene.sizeRestrictions {
let screen = winScene.screen
let size = screen.nativeBounds.size
sizes.minimumSize = size
sizes.maximumSize = size
}
#endif
}
In this example, the screen won't be resizable because both the min and max are the same. Adjust to suit the needs of your app. If you give different values for the min and max you can also be combine this with setting the window frame if you want the initial size to be between the min and max setting.
Here is the same solution in Objective-C:
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
if (![scene isKindOfClass:[UIWindowScene class]]) { return; }
UIWindowScene *winScene = (UIWindowScene *)scene;
#if TARGET_OS_MACCATALYST
UISceneSizeRestrictions *sizes = winScene.sizeRestrictions;
if (sizes) {
UIScreen *screen = winScene.screen;
CGSize size = screen.nativeBounds.size;
sizes.minimumSize = size;
sizes.maximumSize = size;
}
#endif
回答2:
Yes it is possible to start in full screen.
It is a little bit tricky but it is possible. 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.
来源:https://stackoverflow.com/questions/58526250/is-it-possible-to-use-full-screen-in-mac-catalyst