如何在DocumentGroup应用主入口中用#available适配windowResizability
解决macOS 13+仅启用
.windowResizability(.contentSize)的问题 直接给链式调用添加@available修饰符无效,因为该修饰符无法用于这种链式调用场景。你可以通过以下两种方式实现需求:
方法一:运行时版本判断(推荐)
用@ViewBuilder封装方法,在内部根据系统版本决定是否添加修饰符,确保APP在不同版本macOS上都能正常运行:
@main struct Test_HowAviableApp: App { var body: some Scene { makeDocumentScene() } @ViewBuilder private func makeDocumentScene() -> some Scene { let baseScene = DocumentGroup(newDocument: MyDocument()) { file in MyView(document: file.$document) } if #available(macOS 13.0, *) { baseScene.windowResizability(.contentSize) } else { baseScene } } }
方法二:编译时条件判断
如果项目仅针对特定SDK版本编译,可使用条件编译指令,在编译阶段就决定是否添加修饰符:
@main struct Test_HowAviableApp: App { var body: some Scene { DocumentGroup(newDocument: MyDocument()) { file in MyView(document: file.$document) } #if os(macOS) && swift(>=5.7) // Swift 5.7对应macOS 13 SDK .windowResizability(.contentSize) #endif } }
内容的提问来源于stack exchange,提问作者Sébastien REMY
相关产品推荐
相关产品推荐

