ios - 仅将 SidebarListStyle() 应用于 Mac

标签 ios swift swiftui catalyst

在我的 Catalyst 项目中,我想将 SidebarListStyle() 仅应用于 Mac。

我的问题是我无法构建项目,即使在检查操作系统时也是如此。这是一个例子:

struct CrossList: View {
    #if targetEnvironment(macCatalyst)
    var body: some View {
        List {
            Text("Mac Test")
        }
        .listStyle(SidebarListStyle())
    }
    #else
    var body: some View {
        List {
            Text("iOS Test")
        }
    }
    #endif
}

构建时出现以下错误:

'SidebarListStyle' is unavailable in iOS

最佳答案

Mac Catalyst 本质上是 iOS – 只是在 Mac 上运行。 SidebarListStyle 仅在开发完整的 macOS(非 Catalyst)应用程序时可用,并且将包含在编译器指令 #if os(macOS) 中,如下所示:

struct CrossList: View {
    #if os(macOS)
    var body: some View {
        List {
            Text("Mac Test")
        }
        .listStyle(SidebarListStyle())
    }
    #else
    var body: some View {
        List {
            Text("iOS Test")
        }
    }
    #endif
}

关于ios - 仅将 SidebarListStyle() 应用于 Mac,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58883496/

相关文章:

ios - SwiftUI 选择器填充问题 - 选择器中的 ForEach 循环未填充

iphone - 如何将数组传递给方法,然后确定数组的大小?

ios - 如何将 NSString 转换为 int

ios - UITableViewCell内部的动态高度收集/标签 View

swift - 为什么保存被管理对象上下文变化的是Deleted值?

ios - SwiftUI-带有可点击按钮的单元格(在Form内)

类似于Android AlarmManager的iOS后台进程

ios - swift:无法在 Playground 上将(本地)plist 文件读取到字典

swift - 配置 Interface Builder 的新 WKWebView 对象

json - 使用 Codable 将 String 元素转换为复杂类型