swift - Nested TabView - 删除内部标签栏 iOS 13,Swift UI
问题描述
我使用 aTabView
来表示三个选项卡。在 iOS 14 中,这处理得很好,但是 iOS 13 导致一个灰色的底栏,它是导航的标签栏。我怎样才能删除这个栏?
请记住,这是一个TabView
在一个内TabView
。外部TabView
的栏显示在底部,有五个选项卡;TabView
我根本不想显示的内栏。
下面的代码代表内部TabView
。
import SwiftUI
struct DashboardView: View {
@State private var pageIndex = 1
var body: some View {
VStack {
DashboardTopBar(index: $pageIndex) // A custom selected tab indicator
if #available(iOS 14.0, *) {
TabView(selection: $pageIndex) {
RehabView().tag(0)
PerformanceView().tag(1)
RecoveryView().tag(2)
}
.tabViewStyle(PageTabViewStyle())
.indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
} else {
TabView(selection: $pageIndex) {
RehabView().tag(0)
PerformanceView().tag(1)
RecoveryView().tag(2)
}
}
}
}
}
下图左侧为 iOS 13,右侧为 iOS 14。
最小、完整和可验证的示例
如果下面的代码在 iOS 13 上启动,您会注意到底部有一个标签栏,顶部有一个空标签栏。
import SwiftUI
struct ContentView: View {
@State private var outerTabViewSelectedTab = 0
@State private var innerTabViewSelectedTab = 1
var body: some View {
TabView(selection: $outerTabViewSelectedTab,
content: {
Text("Outer 1").tabItem { Text("Outer 1") }.tag(1)
TabView(selection: $innerTabViewSelectedTab,
content: {
Text("Inner 1").tag(1)
Text("Inner 2").tag(2)
}).tabItem { Text("Outer 2") }.tag(2)
})
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
解决方案
Hide it explicitly... as it is for backward compatibility it is safe, because behavior is already known and won't change.
So here is a solution
var body: some View {
TabView(selection: $outerTabViewSelectedTab,
content: {
Text("Outer 1").tabItem { Text("Outer 1") }.tag(1)
TabView(selection: $innerTabViewSelectedTab,
content: {
Text("Inner 1").tag(1)
.background(TabBarAccessor { tabBar in
tabBar.isHidden = true
})
Text("Inner 2").tag(2)
}).tabItem { Text("Outer 2") }.tag(2)
})
}
Used TabBarAccessor
from my other solution in https://stackoverflow.com/a/59972635/12299030
推荐阅读
- python - 具有复数元素的 Numpy 矩阵向量乘法
- reactjs - Reactjs屏幕不刷新,更新时总是落后一个循环
- arrays - 在常见的 lisp 中,使用 '#(...) 或仅使用 #(...) 创建向量有什么区别?
- python - SQL 中的中间聚合函数和数据操作
- azure - 有没有办法使用二头肌将 Azure 防火墙连接到 Front Door 高级策略?
- swift - xcode12 如何将.md 文件的字体更改为等宽字体?
- javascript - 展平嵌套的 JSON 对象
- python - 一次将多个 tkinter 菜单项设置为“正常”的最有效方法是什么?
- python - 你如何在 Numpy 中创建一个(有时)参差不齐的数组?
- xcode - 为什么 datePicker View 的转换不能按预期工作?