[英]SwiftUI TabView not rendering Preview Labels
I am developing a SwiftUI App for macOS.我正在为 macOS 开发一个 SwiftUI 应用程序。
However, i was not able to make TabView
to render correctly in Preview Canvas and when using the view on a new Window.但是,在预览 Canvas 以及在新的 Window 上使用视图时,我无法使TabView
正确呈现。
struct DevView: View {
@State var isOn = true
var body: some View {
TabView {
Toggle(isOn: $isOn) {
Text("Foo")
}
.toggleStyle(SwitchToggleStyle(tint: .green))
.tabItem {
Label("Foo", systemImage: "dot.square")
}
Text("Bar")
.tabItem {
Label("Bar", systemImage: "gear")
}
}.frame(width: 200, height:75)
}
}
struct DevView_Previews: PreviewProvider {
static var previews: some View {
DevView()
}
}
Why does this Code renders like this in Preview Canvas:为什么此代码在预览版 Canvas 中呈现如下:
instead of correctly showing the tabs with labels:而不是正确显示带有标签的选项卡:
For the record: XCode 13.2.1, BigSur 11.6.2备案:XCode 13.2.1,BigSur 11.6.2
On MacOS SwiftUI does not render images in Tab labels: https://developer.apple.com/design/human-interface-guidelines/macos/windows-and-views/tab-views/在 MacOS 上 SwiftUI 不会在标签标签中呈现图像: https://developer.apple.com/design/human-interface-guidelines/macos/windows-and-views/tab-views/
You have to go for a custom solution with Buttons.您必须 go 才能获得带有按钮的自定义解决方案。 This seems to be tricky.这似乎很棘手。 The only way I got half way was the following.我获得一半的唯一方法是以下。
This basically is a customizable ToolBar.这基本上是一个可定制的工具栏。 To not have it in the title bar, you have to use.windowToolbarStyle(.expanded).要在标题栏中没有它,您必须使用.windowToolbarStyle(.expanded)。 Unfortunately the.windowToolbarStyle(.expanded) has to be directly on the WindowGroup:不幸的是,.windowToolbarStyle(.expanded) 必须直接在 WindowGroup 上:
@main
struct test_stack_macOsApp: App {
var body: some Scene {
WindowGroup {
DevView()
.toolbar(id: "myToolbar") {
ToolbarItem(id: "1", placement: .principal) {
Button {
// pass id to view
} label: {
Label("Foo", systemImage: "dot.square")
}
}
ToolbarItem(id: "2") {
Button {
// pass id to view
} label: {
Label("Bar", systemImage: "gear")
}
}
}
.navigationTitle("Foo")
}
.windowToolbarStyle(.expanded)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.