我试图将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()
您也可以通过获取根导航或选项卡视图控制器并将其居中放置在其中来完成此操作。
关于ios - Swift 4 UIActivityIndicatorView不在UITableView的中心,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53049504/