iosswiftuiviewuiactivityindicatorviewuialertcontroller

How to display activity indicator in center of UIAlertController?


I currently have a UIAlertController being displayed on the screen. The view of the alert should only display 2 elements, a title and a UIActivityIndicatorView in the center of the alert. Below is the function that displays the alert and its elements.

func displaySignUpPendingAlert() -> UIAlertController {
        //Create the UIAlertController
        let pending = UIAlertController(title: "Creating New User", message: nil, preferredStyle: .Alert)
        //Create the activity indicator to display in it.
        let indicator = UIActivityIndicatorView(frame: CGRectMake(pending.view.frame.width / 2.0, pending.view.frame.height / 2.0, 20.0, 20.0))
        indicator.center = CGPointMake(pending.view.frame.width / 2.0, pending.view.frame.height / 2.0)
        //Add the activity indicator to the alert's view
        pending.view.addSubview(indicator)
        //Start animating
        indicator.startAnimating()

        self.presentViewController(pending, animated: true, completion: nil)
        return pending
    }

However, the activity indicator doesn't display in the center of the view, in fact it displays in the bottom right of the screen, far off of the view. What is the reason for this?

EDIT: I understand that I can hardcode numbers for the indicator's position, but I want the alert to work on multiple devices with multiple screen sizes and orientations.


Solution

  • Be sure to set the frame property when you're creating a view.

    func displaySignUpPendingAlert() -> UIAlertController {
            //create an alert controller
            let pending = UIAlertController(title: "Creating New User", message: nil, preferredStyle: .Alert)
    
            //create an activity indicator
            let indicator = UIActivityIndicatorView(frame: pending.view.bounds)
            indicator.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    
            //add the activity indicator as a subview of the alert controller's view
            pending.view.addSubview(indicator)
            indicator.isUserInteractionEnabled = false // required otherwise if there buttons in the UIAlertController you will not be able to press them
            indicator.startAnimating()
    
            self.presentViewController(pending, animated: true, completion: nil)
    
            return pending
    }
    

    To @62Shark:

    let pending = UIAlertController(title: "Creating New User", message: nil, preferredStyle: .Alert)
    
    let indicator = UIActivityIndicatorView()
    indicator.setTranslatesAutoresizingMaskIntoConstraints(false)
    pending.view.addSubview(indicator)
    
    let views = ["pending" : pending.view, "indicator" : indicator]
    var constraints = NSLayoutConstraint.constraintsWithVisualFormat("V:[indicator]-(-50)-|", options: nil, metrics: nil, views: views)
    constraints += NSLayoutConstraint.constraintsWithVisualFormat("H:|[indicator]|", options: nil, metrics: nil, views: views)
    pending.view.addConstraints(constraints)
    
    indicator.userInteractionEnabled = false
    indicator.startAnimating()
    
    self.presentViewController(pending, animated: true, completion: nil)