简体   繁体   English

SwiftUI:在主视图上显示模态(实际上不是模态)视图时,如何限制视图的可点击区域?

[英]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.

相关问题 呈现视图控制器之上的模态视图控制器 - Modal view controller over Presenting view controller 为什么在 SwiftUI 中,当用 .sheet 呈现模态视图时,init() 被调用两次 - Why is it in SwiftUI, when presenting modal view with .sheet, init() is called twice 在部分页面卷曲模式视图上呈现模式视图 - Presenting modal view over partial page curl modal view 如果当前视图控制器发生更改,如何关闭模式视图控制器? - How can a modal view controller be dismissed if the presenting view controller is changed? Swift 2.0在TableView上呈现模式视图 - Swift 2.0 Presenting modal view over tableview 在弹出窗口上显示模式视图时,不会在主视图中调用ViewDidAppear - ViewDidAppear not called in main view when modal view is presented over a popover 呈现模式视图控制器时的异常:NSInternalInconsistencyException - Exception when presenting modal view controller: NSInternalInconsistencyException 如何在 SwiftUI 中实现下拉以关闭模态视图? - How can I implement a pull down to dismiss a modal view in SwiftUI? 如何自定义Modal View Controller呈现动画? - How to custom Modal View Controller presenting animation? 延迟呈现模式视图控制器 - Delay in presenting a modal view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM