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

标签 ios swift swiftui

我正在开发一个基于具有三个 TabItem 的 Tabview 的应用程序。每个 TabItem 都是一个列表,我将能够在这些列表上显示一种模态视图。当我无法将工作表称为模态视图时,问题就出现了,因为工作表几乎是全窗口的。我需要某种底部模态视图,因此我创建了一个 View ,将其呈现在具有较高 ZIndex 的列表上。它似乎一直有效,直到您单击选项卡栏并选择另一个部署了“模态” View 的 TabItem。错误是:

[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). This may cause bugs by forcing views inside the table view to load and perform layout without accurate information (e.g. table view bounds, trait collection, layout margins, safe area insets, etc), and will also cause unnecessary performance overhead due to extra layout passes.

所以,我想作为解决方案将可点击区域限制为“模态” View 区域。有没有办法实现这个目标?

enter image description here

最佳答案

可能您有一些条件状态,具体取决于您呈现的“类似模态” View ,因此根据相同的条件,您可以在 TabView 下方禁用,如下所示

TabView {
// ... tabs content here
}.disabled(showingModal)

更新:这是我所说的方法演示(使用 Xcode 11.3+ 进行测试)

enter image description here

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 })
            }
        }
    }
}

关于ios - SwiftUI:在主视图上呈现模态(实际上不是模态) View 时,如何限制 View 的可点击区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60110031/

相关文章:

ios - 使用动态原型(prototype)单元在 segue 上将 CloudKit 记录从 TableViewController A 传递到 TableViewControllerB

ios - 如何下载 pdf 文件,在 iPhone 的 UITableViewCell 中显示进度条和当前状态

swift - 将分段样式选择器添加到SwiftUI的NavigationView中

image - 使用 DispatchQueue 在 SwiftUI 中异步加载图像

ios - LocalNotification-iOS 7.1模拟器上的UILocalNotificationDefaultSoundName不起作用

C const void * 参数来自 Swift : Data? .withUnsafeBytes 和 UnsafeRawPointer

ios - 将计时器标签格式化为小时 :minutes:seconds in Swift

swift - Sqlite.swift 缺少标签 "where"

swift - 在 SwiftUI 中强制 View 位于 View 的右下角

iphone - SQLite Group By 和 Order By 不起作用