问题
I have a UIActivityIndicatorView
to my table view in the interface designer. I am trying to center it in the middle of the screens using the following:
override func viewWillLayoutSubviews() {
self.activityView.center = self.view.center
}
But it always seems to be sitting too 'low' on the page, it appears to be centered in the middle of the UITableView, not in the middle of the window like I want (ie. It is getting pushed down by the navigation bar etc)
回答1:
The solution was to set the activityView frame to the bounds of the view. Setting just the center doesn't work because the y value for the tableView frame is negative.
override func viewWillLayoutSubviews() {
self.activityView.translatesAutoresizingMaskIntoConstraints = true
self.activityView.frame = self.view.bounds
}
回答2:
Have you tried:
self.activityView.center = self.view.window?.center
If not then how about this:
let screenSize = UIScreen.main.bounds
self.activityView.center = CGPoint(x: screenSize.width / 2, y: screenSize.height / 2)
It's a bit longer but handles In-Call Status Bar well.
来源:https://stackoverflow.com/questions/41886374/center-a-uiactivityindicatorview-for-a-uitableviewcontroller-in-the-window-not