5

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.

Previously in UIKit, it would be this simple:

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:

enter image description here

A simple SwiftUI based version can be created as such:

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:

enter image description here

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:

dismissButton: .default(Text(""))) 

enter image description here

Based on what I have seen, it does not appear that the Alert in SwiftUI supports what I want, based upon inspecting its initializers.

/// 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.

My take is either we will have to pull in a UIKit based AlertController, or use a different effect to indicate status. However, I'd love to be wrong here.

2
  • By any chance, did you get the solution @CodeBender? Commented Apr 14, 2023 at 18:44
  • I actually got the behavior you're looking for, although for me it was a bug. I was setting the role to .cancel for an alert with a single Button. Commented Jul 14, 2023 at 15:01

2 Answers 2

1

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) } 
Sign up to request clarification or add additional context in comments.

Comments

0

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.

This library might help you out: https://github.com/exyte/PopupView

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.