swiftui - 在 swiftui 中禁用或忽略 TabView 上的点击
问题描述
我有一个非常普通的应用程序,带有TabView
. 但是,当某个内容视图中发生特定过程时,我想阻止用户切换选项卡,直到该过程完成。
如果我disabled
在其自身上使用该属性TabView
(使用@State
绑定来驱动它),那么整个内容视图似乎被禁用 - 水龙头似乎没有通过主视图上的按钮。
例子:
struct FooView: View {
var body: some View {
TabView {
View1().tabItem(...)
View2().tabItem(...)
}
.disabled(someStateVal)
}
}
显然,我希望View1
仍然允许用户,你知道,做事。当someStateVal
为真时,整体View1
没有反应。
有没有办法防止基于更改标签someStateVal
?
谢谢!
解决方案
I could not find a way to individually disable a tabItem, so here is an example idea until someone comes up with more principled solution.
The trick is to cover the tab bar with a clear rectangle to capture the taps.
struct ContentView: View {
@State var isBusy = false
var body: some View {
ZStack {
TabView {
TestView(isBusy: $isBusy)
.tabItem {Image(systemName: "globe")}
Text("textview 2")
.tabItem {Image(systemName: "info.circle")}
Text("textview 3")
.tabItem {Image(systemName: "gearshape")}
}
VStack {
Spacer()
if isBusy {
Rectangle()
.fill(Color.white.opacity(0.001))
.frame(width: .infinity, height: 50)
}
}
}
}
}
struct TestView: View {
@Binding var isBusy: Bool
var body: some View {
VStack {
Text("TestView")
Button(action: {
isBusy.toggle()
}) {
Text("Busy \(String(isBusy))").frame(width: 170, height: 70)
}
}
}
}
推荐阅读
- javascript - React Native 导航中全屏模式的“无法读取未定义的属性导航”
- r - Ubuntu AWS 和 Ubuntu 笔记本电脑中 R 程序的不同行为
- python - Python:使用循环遍历电子表格的一列,将其插入 url,然后保存数据
- assembly - 我有一个问题要求使用 MARS 4.5 模拟器使用 MIPS 汇编语言反转数组
- excel - 为什么有时一行代码可以工作但下一行代码不行
- r - 如何轻松显示 ggplot 的 geom_smooth 背后的方程
- reactjs - React Todo App - typeof a props 不符合我的预期
- reactjs - 在前端和后端之间发送请求时如何防止我的秘密数据(例如来自“中间人攻击”)?
- r - getSymbols 返回不准确的数据
- android - 在 null Flutter 上调用了 getter 'length'