简体   繁体   English

在窗口中将 UITableViewController 的 UIActivityIndi​​catorView 居中,而不是在表视图中

[英]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:使用自动布局,您可以使用layoutMarginsGuideUITableViewUICollectionView获得正确的中心 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.

相关问题 UITableViewController中的UIActivityIndi​​catorView - UIActivityIndicatorView in a UITableViewController 在UITableViewController的UITableView的相同层次结构中的UIActivityIndi​​catorView - UIActivityIndicatorView in the same hierarchy of UITableView in UITableViewController 如何在UITableViewController中显示UIActivityIndi​​catorView? - How to show an UIActivityIndicatorView whitin UITableViewController? 以编程方式为UITableViewController设置UIActivityIndi​​catorView - Setting up an UIActivityIndicatorView for the UITableViewController programmatically UITableViewController 之外的静态表格视图 - Static table view outside UITableViewController iOS如何在UITableViewController上向屏幕中心添加视图并将其粘贴到屏幕中心? - iOS how to add a view to screen center and stick it to screen center on a UITableViewController? 在UITableViewController iOS中实现另一个表视图 - Implementing another table view in a UITableViewController ios 内置表格视图的UITableViewController和UIViewController之间切换 - Switching between UITableViewController and UIViewController with table view inside UITableViewController中的节标题和表格视图单元的问题 - Issue with section header and table view cell in UITableViewController UIActivityIndi​​catorView视图上的对话框 - UIActivityIndicatorView dialog over view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM