[英]Center a UIActivityIndicatorView for a UITableViewController in the window, not in the table view
I have a UIActivityIndicatorView
to my table view in the interface designer.我在界面设计器中的表格视图中有一个
UIActivityIndicatorView
。 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)但它似乎总是在页面上的位置太“低”,它似乎位于 UITableView 的中间,而不是像我想要的那样位于窗口中间(即它被导航栏等向下推) )
The solution was to set the activityView frame to the bounds of the view.解决方案是将 activityView 框架设置为视图的边界。 Setting just the center doesn't work because the y value for the tableView frame is negative.
仅设置中心不起作用,因为 tableView 框架的 y 值为负。
override func viewWillLayoutSubviews() {
self.activityView.translatesAutoresizingMaskIntoConstraints = true
self.activityView.frame = self.view.bounds
}
Using auto layout, you can use layoutMarginsGuide
to get the right center X, Y anchor in both UITableView
s and UICollectionView
s:使用自动布局,您可以使用
layoutMarginsGuide
在UITableView
和UICollectionView
获得正确的中心 X、Y 锚点:
activityView.translatesAutoresizingMaskIntoConstraints = false
activityView.centerXAnchor.constraint(equalTo: view.layoutMarginsGuide.centerXAnchor).isActive = true
activityView.centerYAnchor.constraint(equalTo: view.layoutMarginsGuide.centerYAnchor).isActive = true
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.它有点长,但可以很好地处理通话中状态栏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.