swiftui - macCatalyst/ SwiftUI 触控栏
问题描述
如何在用 SwiftUI 编写的 Catalyst- 应用程序中添加触控栏支持?
例如,如果我想在视图中显示一个按钮:
import SwiftUI
struct ContentView: View {
var body: some View {
VStack{
#if targetEnvironment(macCatalyst)
Text("macOS")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.focusable()
.touchBar {
Button(action: {
print("tapped")
}) {
Text("TestButton")
}
}
#endif
Text("iOS")
}
}
}
如果我在 macOS 应用程序中使用它,它可以工作,但如果我在 Catalyst 中使用它并添加 targetEnvironment,则会出现错误:
'focusable(_:onFocusChange:)' 在 iOS 中不可用,而 'touchBar(content:)' 在 iOS 中不可用
谢谢你的帮助。
解决方案
您可以添加自定义视图修饰符:
struct TouchBarView: ViewModifier {
func body(content: Content) -> some View {
#if os(macOS)
return content
.touchBar { ... }
#else
return content
#endif
}
}
用法:
Text("Text")
.modifier(TouchBarView())
推荐阅读
- angular - 每隔 1 秒更新一次 ngFor 模板的数组
- php - Laravel 自定义字符串帮助文件
- .net-core - 使用 RabbitMQ 发布/订阅微服务事件总线时如何去重事件
- visual-studio-code - 如何在代码编辑器和我的 Nativescript 项目中解决这个新的 CCS 错误
- apache-zeppelin - 在 docker 映像中运行 zeppelin 时如何为解释器设置用户解释?
- google-sheets - 在 Google 表格中将文本重新格式化为数字的更简单方法
- python - 在python中用gdal读取ecw文件?
- css - CSS获取类的最后一个孩子
- terraform - 使用版本控制创建 GCS 存储桶
- javascript - 使用 PDF.js 异步加载多个 pdf 文件时获取总页数