AaoIi
AaoIi

Reputation: 8396

Adjust UIActivityIndicatorView position in AlertView - Swift

I've been trying to adjust the position of IndicatorView inside UIAlertView, but it never works. i tried to set for loadingIndicator loadingIndicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50) but it didn't apply at all, take a look on this photo :

enter image description here

alert = UIAlertView(title: "Loading...", message: nil , delegate: nil, cancelButtonTitle: nil)
        loadingIndicator = UIActivityIndicatorView(frame: CGRectMake(50, 10, 37, 37))
        loadingIndicator.center = self.view.center
        loadingIndicator.hidesWhenStopped = true
        loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
        loadingIndicator.startAnimating();

        alert.setValue(loadingIndicator, forKey: "accessoryView")
        loadingIndicator.startAnimating()
        alert.show();

Upvotes: 1

Views: 3753

Answers (3)

tomasdev
tomasdev

Reputation: 5997

You can use as many \n as necessary in the message to create the space.

In your ViewController.swift:

lazy var alertController: UIAlertController = {
    let alert = UIAlertController(title: "Loading", message: "\n\n", preferredStyle: .alert)
    alert.view.tintColor = .black
    let loading = UIActivityIndicatorView(frame: CGRect(x: 110, y: 35, width: 50, height: 50))
    loading.hidesWhenStopped = true
    loading.style = .gray
    loading.startAnimating();
    alert.view.addSubview(loading)
    return alert
}()

And usage:

// to show loading wheel
present(alertController, animated: true)

// to hide loading wheel
dismiss(animated: true)

Visual output:

Image showing a popup with the message Loading

(Inspired by @AshishKakkad answer)

Upvotes: 0

Ashish Kakkad
Ashish Kakkad

Reputation: 23882

enter image description here

Try following code:

        var alert : UIAlertView = UIAlertView(title: "Loading...", message: nil , delegate: nil, cancelButtonTitle: nil)
        var viewBack:UIView = UIView(frame: CGRectMake(83,0,100,100))

        var loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(50, 10, 37, 37))
        loadingIndicator.center = viewBack.center
        loadingIndicator.hidesWhenStopped = true
        loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
        loadingIndicator.startAnimating();
        viewBack.addSubview(loadingIndicator)
        viewBack.center = self.view.center
        alert.setValue(viewBack, forKey: "accessoryView")
        loadingIndicator.startAnimating()
        alert.show();

Upvotes: 5

leizh007
leizh007

Reputation: 19

I tried many times add custom view to UIAlertView ,many strange things happen. You can choose to use this library it is not as beautiful as the UIAlertView, it may solve you problem.

Custom iOS AlertView

Upvotes: 1

Related Questions