Activity 指示器未在 TableView 中居中

Activity indicator is not in centered in TableView

我想在 TableView 中显示 activity indicatorNIB/XIB 文件,但它显示的不在中间,它放在右侧

我愿意:

override func viewDidLoad() {
    super.viewDidLoad()

let actInd: UIActivityIndicatorView = UIActivityIndicatorView()
    actInd.frame = CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0)
    actInd.center = self.view.center
    actInd.hidesWhenStopped = true
    actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
    self.view.addSubview(actInd)
    actInd.startAnimating()
}
let actInd: UIActivityIndicatorView = UIActivityIndicatorView()

override func viewDidLoad() {
    super.viewDidLoad()

    actInd.frame = CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0)
    actInd.hidesWhenStopped = true
    actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
    self.view.addSubview(actInd)
    actInd.startAnimating()
}


override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    actInd.center = self.view.center
}

/* Following two functions are acceptable also.
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    actInd.center = self.view.center
}

override func viewDidLayoutSubviews() { 
    super.viewDidLayoutSubviews()
    actInd.center = self.view.center
}
*/

你可以:

  • 在您的 XIB/Storyboard
  • 中添加 UIActivityIndicator
  • 将其设置为 hidden(在故事板中)
  • 创建 IBOutlet
  • 随时切换 spinner.isHidden show/hide。

这种方法对我有用。

    if let window = UIApplication.shared.keyWindow { 
        activityIndicator.frame = CGRect(x: window.frame.width/2, y: window.frame.height/2, width: 0, height: 0)
        activityIndicator.hidesWhenStopped = true
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.white

        window.addSubview(activityIndicator)
    }

希望对您有所帮助:)