[英]How to present an Alert in SwiftUI with no buttons
I have a situation where I would like to recreate previous UIKit
logic by displaying an Alert
during a long running operation that the user cannot interact with.我有一种情况,我想通过在用户无法与之交互的长时间运行操作期间显示
Alert
来重新创建以前的UIKit
逻辑。
Previously in UIKit, it would be this simple:以前在 UIKit 中,就是这么简单:
import UIKit
class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let alertController = UIAlertController(title: "Loading",
message: nil,
preferredStyle: .alert)
present(alertController, animated: true, completion: nil)
}
}
And it looks like this:它看起来像这样:
A simple SwiftUI
based version can be created as such:可以这样创建一个简单的基于
SwiftUI
的版本:
import SwiftUI
struct ContentView: View {
@State private var isLoading = true
var body: some View {
Text("Hello")
.modifier(LoadingAlert(isPresented: $isLoading))
}
}
struct LoadingAlert: ViewModifier {
@Binding var isPresented: Bool
func body(content: Content) -> some View {
content
.alert(isPresented: $isPresented) {
Alert(title: Text(NSLocalizedString("Loading", comment: "")),
dismissButton: .none)
}
}
}
Whether I use nil
or .none
as the dismissButton
argument, or completely omit the line, it will use a default OK
button, and produce this:无论我使用
nil
或.none
作为dismissButton
参数,还是完全省略该行,它都将使用默认的OK
按钮,并生成以下内容:
I did modify the Alert
arguments, sending in a button with an empty title
, but this is the result, which is not as clean as I would like:我确实修改了
Alert
arguments,发送了一个空title
的按钮,但这是结果,它不像我想要的那样干净:
dismissButton: .default(Text("")))
Based on what I have seen, it does not appear that the Alert in SwiftUI supports what I want, based upon inspecting its initializers.根据我所看到的,基于检查其初始化程序,SwiftUI 中的警报似乎不支持我想要的。
/// Creates an alert with one button.
public init(title: Text, message: Text? = nil, dismissButton: Alert.Button? = nil)
/// Creates an alert with two buttons.
///
/// - Note: the system determines the visual ordering of the buttons.
public init(title: Text, message: Text? = nil, primaryButton: Alert.Button, secondaryButton: Alert.Button)
For this project, the goal is to fully utilize SwiftUI, but it appears this is a scenario where we cannot get the desired result.对于这个项目,目标是充分利用 SwiftUI,但似乎这是一个我们无法获得预期结果的场景。
My take is either we will have to pull in a UIKit based AlertController
, or use a different effect to indicate status.我的看法是要么我们必须引入基于 UIKit 的
AlertController
,要么使用不同的效果来指示状态。 However, I'd love to be wrong here.但是,我很想在这里错了。
I don't think an alert without a dismiss button in currently supported in SwiftUI, but you could create a custom view and present it with the same effect.我不认为 SwiftUI 目前支持没有关闭按钮的警报,但您可以创建自定义视图并以相同的效果呈现它。
This library might help you out: https://github.com/exyte/PopupView这个库可能会帮助你: https://github.com/exyte/PopupView
You can try this:
你可以试试这个:
var dialogMessage = UIAlertController(title: "Erro", message: "error description", preferredStyle: .alert)
let window = UIApplication.shared.keyWindow
window?.rootViewController?.present(dialogMessage, animated: true)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.