©️ OverlookArt
首页 / AppleDeveloper / SwiftUI / 支持 MacOS

支持 MacOS

项目配置

  1. 在 项目的 TARGETS -> General -> Supported Destinations -> Add Destination,
  2. Add Destination:点击 “+” 按钮,打开选择 Destination 的菜单。
  3. 选择 Mac Catalyst 添加到支持的列表内。
  4. 将运行的设备调整为 Mac,点击运行启动应用程序。

TabView 适配

使用 .tabViewStyle(.sidebarAdaptable) 适配 MacOS 的侧边栏样式。

 1
 2struct MainTabView: View {
 3    var body: some View {
 4        TabView {
 5            ...
 6        }
 7    }
 8}
 9
10@main
11struct SUIApp: App {
12    var body: some Scene {
13        WindowGroup {
14            if #available(iOS 18.0, *) {
15                MainTabView()
16                    .tabViewStyle(.sidebarAdaptable)
17            }else {
18                MainTabView()
19            }
20        }
21    }
22}

隐藏标题栏

 1class SceneDelegate: NSObject, UIWindowSceneDelegate {
 2    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
 3        guard let windowScene = scene as? UIWindowScene else { return }
 4        #if targetEnvironment(macCatalyst)
 5        if let titleBar = windowScene.titlebar {
 6            titleBar.titleVisibility = .hidden
 7        }
 8        #endif
 9    }
10}