[英]SwiftUI: How can I restrict the tappable area of a view when presenting a modal(actually not modal) view over a main view?
I am developing an app based on a Tabview with three TabItems.我正在开发基于带有三个 TabItem 的 Tabview 的应用程序。 Each TabItem is a List and I would be able to show a kind of modal view over those Lists.
每个 TabItem 都是一个列表,我将能够在这些列表上显示一种模式视图。 The problem becomes when I can not call a Sheet as modal view because Sheets are almost full windowed.
当我无法将工作表称为模态视图时,问题就变成了,因为工作表几乎是全窗口的。 I need some kind of bottom modal view, so I create a View that I present over a List with higher ZIndex.
我需要某种底部模态视图,所以我创建了一个视图,我在具有更高 ZIndex 的列表上呈现。 It seems to work until you click in the tabbar and select another TabItem having deployed the "modal" view.
它似乎可以工作,直到您单击选项卡栏并选择另一个已部署“模态”视图的 TabItem。 The error is:
错误是:
[TableView] Warning once only: UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window).
[TableView] 仅警告一次:UITableView 被告知在不在视图层次结构中布局其可见单元格和其他内容(表视图或其超视图之一尚未添加到窗口中)。 This may cause bugs by forcing views inside the table view to load and perform layout without accurate information (eg table view bounds, trait collection, layout margins, safe area insets, etc), and will also cause unnecessary performance overhead due to extra layout passes.
这可能会通过强制表视图中的视图加载和执行没有准确信息(例如表视图边界、特征集合、布局边距、安全区域插入等)的视图来导致错误,并且还会由于额外的布局传递而导致不必要的性能开销.
So, I would like as solution to restrict the tappable area to the "modal" view area.所以,我想作为将可点击区域限制为“模态”视图区域的解决方案。 ¿Is there a way to achieve this?
¿有没有办法实现这一目标?
Probably you have some condition state depending on which you present your "modal-like" view, so depending on the same condition you can disable below TabView, like below可能您有一些条件状态,具体取决于您呈现“类似模态”的视图,因此根据相同的条件,您可以在 TabView 下方禁用,如下所示
TabView {
// ... tabs content here
}.disabled(showingModal)
Update: Here is a demo of approach that I meant (tested with Xcode 11.3+)更新:这是我的意思的方法演示(使用 Xcode 11.3+ 测试)
struct TestTabViewModal: View {
@State private var selectedTab = 0
@State private var modalShown = false
var body: some View {
ZStack {
TabView(selection: $selectedTab) {
VStack {
Button("Show Modal") { self.modalShown = true }
.padding(.top, 40)
Spacer()
}
.tabItem {
Image(systemName: "1.circle")
}.tag(0)
Text("2").tabItem {
Image(systemName: "1.circle")
}.tag(1)
}.disabled(modalShown)
if modalShown {
RoundedRectangle(cornerRadius: 10)
.fill(Color.yellow)
.frame(width: 320, height: 240)
.overlay(Button("CloseMe") { self.modalShown = false })
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.