我试图将UIActivityIndicatorView放在UITableView中,这就是我创建UIActivityIndicatorView的方法:
indicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) as UIActivityIndicatorView
//Set the activity indictor center
indicator.center = self.view.center
//Hide the indicator when its stopped.
indicator.hidesWhenStopped = true
//Set the style of the activity indicator
indicator.style = UIActivityIndicatorView.Style.white
//Set the background colour of the activity indicator
indicator.backgroundColor = UIColor(white: 0.0, alpha: 0.6)
//Make the activity indicator have rounded corners
indicator.layer.cornerRadius = 15
//Add activity indicator to view
self.view.addSubview(indicator)
//Start activity indicator
self.indicator.startAnimating()
但是当我向上滚动我的UITableView时,我看不到我的UIActivityIndicatorView,我尝试了以下内容:
override func viewWillLayoutSubviews() {
self.indicator.center = self.view.center
}
但那没用。
我也尝试过:
override func viewWillLayoutSubviews() {
self.indicator.translatesAutoresizingMaskIntoConstraints = true
self.indicator.frame = self.view.bounds
self.indicator.center = self.view.center
}
也没用,我做错了什么?
当我向下滚动,然后在表格视图中选择一个项目然后将出现活动指示器时,会发生这种情况。
你不能在UITableViewController中居中,因为它是一个滚动视图,滚动视图是View Controller的视图。但是,您可以将其置于窗口视图中,如下所示:
// get the visible window
let window = UIApplication.shared.keyWindow!
let viewActivity = UIActivityIndicatorView(style: .whiteLarge)
// set your activity indicator's center to the center of the screen
viewActivity.center = window.center
viewActivity.hidesWhenStopped = true
// add the indicator to the active window (not your uitableviewcontroller)
// and make sure it is at the front (so it is visible)
window.addSubview(viewActivity)
window.bringSubviewToFront(viewActivity)
viewActivity.startAnimating()
您也可以通过抓取根导航或制表视图控制器并将其集中在那里来实现此目的。