尽管我在viewDidLoad中编写了以下代码,但无法在静态表格视图的顶部显示 Activity 指示器。

let activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
tableView.backgroundView = activityIndicatorView
tableView.separatorStyle = UITableViewCellSeparatorStyle.none
self.activityIndicatorView = activityIndicatorView
self.activityIndicatorView.isHidden = false
self.tableView.addSubview(activityIndicatorView)
self.activityIndicatorView.startAnimating()

最佳答案

无需在UIActivityIndicatorView中添加tableView,而是将其添加到主视图内并在主视图上调用bringSubview(toFront:)以将indicatorView置于最前面,并且您也未设置indicatorView集合的原点位置。

self.activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
tableView.separatorStyle = UITableViewCellSeparatorStyle.none
self.activityIndicatorView.isHidden = false

//Add inside your view
self.view.addSubview(activityIndicatorView)

//Set activityIndicatorView origin in the screen
self.activityIndicatorView.center = self.view.center

//Try to bring activityIndicatorView to front
self.view.bringSubview(toFront: activityIndicatorView)

self.activityIndicatorView.startAnimating()

10-08 17:14