How to display activity indicator in center of UIAlertController?

后端 未结 13 1089
猫巷女王i
猫巷女王i 2020-12-08 05:18

I currently have a UIAlertController being displayed on the screen. The view of the alert should only display 2 elements, a title and a UIActivityIndicato

13条回答
  •  遥遥无期
    2020-12-08 05:32

    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)
    

提交回复
热议问题