центрировать представление индикатора в alertcontroller программно - PullRequest
0 голосов
/ 26 июня 2018

Я пытаюсь программно центрировать индикатор активности в контроллере предупреждений, но не вижу ожидаемого результата.

Попытка:

import NVActivityIndicatorView

public func displayActivityAlertWithCompletion(ViewController: UIViewController, pending: UIAlertController, completionHandler: @escaping ()->())
{

    //create an activity indicator
    let indicator = NVActivityIndicatorView(frame: CGRect(x: (pending.view.frame.width/2) - 25 , y: 0, width: 50, height: 50))


    //indicator.clipsToBounds = true
    indicator.type = .ballScaleMultiple
    indicator.autoresizingMask = \[.flexibleWidth, .flexibleHeight\]
    indicator.color = UIColor(rgba: Palette.loadingColour)



    //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()



    ViewController.present(pending, animated: true, completion: completionHandler)
}

enter image description here

1 Ответ

0 голосов
/ 26 июня 2018

Проблема, с которой вы сталкиваетесь, заключается в этой строке:

let indicator = NVActivityIndicatorView(frame: CGRect(x: (pending.view.frame.width/2) - 25 , y: 0, width: 50, height: 50))

На данный момент ваш pending контроллер предупреждений не еще не представлен, и его кадр не обновляется.

Вам нужно будет обновить frame индикатора после того, как контроллер оповещений был представлен, поэтому, например, вы можете сделать следующее.

Заменить эту строку:

ViewController.present(pending, animated: true, completion: completionHandler)

При этом:

ViewController.present(pending, animated: true, completion: {
    // update frame here:
    indicator.frame = CGRect(x: (pending.view.frame.width/2) - 25 , y: 0, width: 50, height: 50)
    // and manually call completionHandler:
    completionHandler()
})
...