【问题标题】:SwiftUI confirmationDialog on a full screen View?全屏视图上的 SwiftUI 确认对话框?
【发布时间】:2021-11-20 00:55:01
【问题描述】:

我正在尝试使用 confirmationDialog 修饰符(iOS 15 中的新功能)。我想我可以像sheet 一样使用它,但似乎你必须将它附加到屏幕某处的一个小视图上(至少在我的 iPad 上)。有没有办法让一个在屏幕中间显示为未附加?

例如,这在 iPad 上工作。对话框是不可见的。

struct ContentView: View {
    @State var showConf = false
    var body: some View {
        VStack(spacing: 0) {
            Spacer(minLength: 20)
            Button("Test Conf. Dialog") {
                showConf = true
            }
            Spacer()
        }
        .frame(maxWidth: .infinity)
        .background(Color.blue.opacity(0.3))
        .confirmationDialog("Test", isPresented: $showConf) {
            Button("Yes") {
                print("Yes!")
            }
        }
    }
}

【问题讨论】:

  • confirmationDialog 是一个 popover per Documentation 并且 SwiftUI 或 UIKit 中的弹出框必须附加到 sourceView 或者你得到一些错误说它没有附加。您可能正在寻找 AlertconfirmationDialog。效果/错误通常只在 iPad 或 Mac 中可见。
  • 好的,谢谢。我想要alertsheetalertconfirmationDialog 似乎都将“消息”部分限制为单个文本。我想要更多,所以可能需要sheet。不幸的是,在 iPad 上控制工作表大小似乎仍然很痛苦。
  • 如果您创建CustomAlert struct/class 并使用.alert(item:),您可以将Alert 内容更改为您想要的任何内容。看到这个SO question我为另一个用例写的
  • Alert 结构已弃用。如果我使用将消息作为参数的alert(...) 修饰符,并且我传递了一个VStack,那么它会忽略堆栈中除了第一个Text 之外的所有内容。这就是我现在在运行 iPadOS 15 的 iPad 上看到的。

标签: swiftui


【解决方案1】:

试试这个,将confirmationDialog“附加”到Button

struct ContentView: View {
    @State var showConf = false
    var body: some View {
        VStack(spacing: 0) {
            Spacer(minLength: 20)
            Button("Test Conf. Dialog") {
                showConf = true
            }
            .confirmationDialog("Test", isPresented: $showConf) {
                Button("Yes") {
                    print("Yes!")
                }
            }
            Spacer()
        }
        .frame(maxWidth: .infinity)
        .background(Color.blue.opacity(0.3))
    }
}

【讨论】:

  • 是的,这行得通,但我的问题是是否有一种方法可以使一个显示为“未附加在屏幕中间”。我猜不会。我需要使用alertsheet 或自定义的东西。该工作表在 iPad 上给了我另一个问题 - 我无法控制大小......但这是另一个 stackoverflow 问题。
猜你喜欢
  • 2015-01-20
  • 2023-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 2014-11-17
相关资源
最近更新 更多