Toolbar items在macOS上更改TabView选择时未隐藏/消失。

huangapple go评论65阅读模式
英文:

Toolbar items not hiding / disappearing when TabView selection is changed on macOS

问题

The same code runs perfectly on iOS, and the behaviour is as expected where whenever the tabview selection is changed, the toolbar items are updated accordingly. On macOS, the toolbar items do not disappear.

我相同的代码在iOS上运行正常,行为如预期,每当选项卡视图的选择更改时,工具栏项目会相应更新。在macOS上,工具栏项目不会消失。

I tried going through all the docs, and I couldn't understand how to figure out this issue. My actual app actually has two different SplitNavigationViews as different items for TabViews, and this is the simplest view where this issue is reproducible.

我尝试阅读了所有文档,但我无法理解如何解决这个问题。我的实际应用程序实际上具有两个不同的SplitNavigationViews作为TabViews的不同项,并且这是可以复现此问题的最简单的视图。

英文:

The same code runs perfectly on iOS, and the behaviour is as expected where whenever the tabview selection is changed, the toolbar items are updated accordingly. On macOS, the toolbar items do not disappear.

Toolbar items在macOS上更改TabView选择时未隐藏/消失。

Toolbar items在macOS上更改TabView选择时未隐藏/消失。

import SwiftUI

struct ContentView: View {
    var body: some View {
        TabView {
            Text("Screen 1")
                .toolbar {
                    ToolbarItem {
                        Text("Item 1")
                    }
                }
                .tabItem {
                    Label("Home", systemImage: "house")
                }
            Text("Screen 2")
                .toolbar {
                    ToolbarItem {
                        Text("Item 2")
                    }
                }
                .tabItem {
                    Label("Explore", systemImage: "network")
                }
        }
    }
}

I tried going through all the docs, and I couldn't understand how to figure out this issue. My actual app actually has two different SplitNavigationViews as different items for TabViews, and this is the simplest view where this issue is reproducible.

答案1

得分: 2

以下是您提供的内容的中文翻译:

这一定是与您的(实际)视图层次结构相关的特定问题,导致iOS和macOS之间的行为差异,因为当我测试您的示例代码时,iOS也不会删除工具栏项。

我的理解是.toolbar是可组合的 - 多个视图可以添加自己的工具栏项,而不打算删除/替换其他视图的工具栏项。对于TabView来说并不是很合理,但我可以理解他们为什么没有试图添加隐藏逻辑来让系统来处理这个问题。

因此,您可以使用条件和状态明确告诉系统哪些工具栏项应该可见。

enum Tabs: Hashable {
    case home
    case explore
}

struct ContentView: View {
    @State private var selectedTab = Tabs.home

    var body: some View {
        NavigationSplitView {
            List {
                Text("Lorem ipsum")
            }
        } detail: {
            TabView(selection: $selectedTab) {
                Text("Screen 1")
                    .tabItem {
                        Label("Home", systemImage: "house")
                    }
                    .tag(Tabs.home)
                Text("Screen 2")
                    .tabItem {
                        Label("Explore", systemImage: "network")
                    }
                    .tag(Tabs.explore)
            }
            .toolbar {
                switch selectedTab {
                    case .home:
                        ToolbarItem {
                            Text("Item 1")
                        }
                    case .explore:
                        ToolbarItem {
                            Text("Item 2")
                        }
                }
            }
        }
    }
}

希望这对您有所帮助。

英文:

It must be something specific to your (actual) view hierarchy that is causing the behavior to differ between iOS and macOS, because when I tested your sample code, iOS also does not remove the toolbar item.

Toolbar items在macOS上更改TabView选择时未隐藏/消失。

My understanding is that .toolbar is meant to be composable — multiple views can add their own toolbar items without intending to remove/replace those of other views. It doesn't really make sense for TabView, but I can understand why they haven't tried to add hidden logic to have the system take care of that.

So instead, you can explicitly tell the system which toolbar items should be visible using conditionals and state.

enum Tabs: Hashable {
    case home
    case explore
}

struct ContentView: View {
    @State private var selectedTab = Tabs.home

    var body: some View {
        NavigationSplitView {
            List {
                Text("Lorem ipsum")
            }
        } detail: {
            TabView(selection: $selectedTab) {
                Text("Screen 1")
                    .tabItem {
                        Label("Home", systemImage: "house")
                    }
                    .tag(Tabs.home)
                Text("Screen 2")
                    .tabItem {
                        Label("Explore", systemImage: "network")
                    }
                    .tag(Tabs.explore)
            }
            .toolbar {
                switch selectedTab {
                    case .home:
                        ToolbarItem {
                            Text("Item 1")
                        }
                    case .explore:
                        ToolbarItem {
                            Text("Item 2")
                        }
                }
            }
        }
    }
}

Toolbar items在macOS上更改TabView选择时未隐藏/消失。

Toolbar items在macOS上更改TabView选择时未隐藏/消失。

huangapple
  • 本文由 发表于 2023年4月17日 15:30:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032647.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定