如何隐藏 Mac Catalyst 应用程序的标题栏?

How can I hide the titlebar of a Mac Catalyst app?

这里是官方link:https://developer.apple.com/documentation/uikit/mac_catalyst/removing_the_title_bar_in_your_mac_app_built_with_mac_catalyst?language=objc

我不知道如何将代码翻译成 Objective-C。页面顶部有一个选项,但它不起作用。

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
    guard let windowScene = (scene as? UIWindowScene) else { return }
    
    #if targetEnvironment(macCatalyst)
    if let titlebar = windowScene.titlebar {
        titlebar.titleVisibility = .hidden
        titlebar.toolbar = nil
    }
    #endif

}

SceneDelegate.h 中将此代码添加到 scene:WillConnectToSession:options: 方法:

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    
#if TARGET_OS_MACCATALYST
    // Code specific to Mac.
    
    [(UIWindowScene *)scene titlebar].titleVisibility = UITitlebarTitleVisibilityHidden;
    
    [(UIWindowScene *)scene titlebar].toolbar = nil;
    
#else
    // Code to exclude from Mac.
#endif

}

有效。