xcode 我试图将 UIActivityIndi​​catorView 置于 UITableView 上,但它有上边距

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10050568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:53:19  来源:igfitidea点击:

I am trying to center UIActivityIndicatorView over a UITableView but it has top margin

iphoneobjective-cxcode

提问by Richard Knop

I am using Xcode 4.2 on Snow Leopard. What I am trying to do. Basically, I have a master view controller with a table view. In order to populate the table view I need to call an API and download the information from there.

我在雪豹上使用 Xcode 4.2。我正在尝试做的事情。基本上,我有一个带有表视图的主视图控制器。为了填充表视图,我需要调用 API 并从那里下载信息。

While downloading the data from the API, I would like to show an activity indicator in order to let a user know something is happening and the app is not stuck.

从 API 下载数据时,我想显示一个活动指示器,以便让用户知道正在发生的事情并且应用程序没有卡住。

What I have done is I have a created a new UIView over the table view with alpha 0.5 and put an activity indicator in its middle. This is the code I execute when the master controller view loads:

我所做的是我在 alpha 0.5 的表视图上创建了一个新的 UIView,并在其中间放置了一个活动指示器。这是我在主控制器视图加载时执行的代码:

// Show the activity indicator
self.overlayView = [[UIView alloc] init];
self.overlayView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
self.overlayView.frame = self.tableView.frame;
self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
CGRect frame = self.overlayView.frame;
self.activityIndicator.center = CGPointMake(frame.size.width/2, frame.size.height/2);
[self.overlayView addSubview:self.activityIndicator];
[self.activityIndicator startAnimating];
[self.tableView addSubview:self.overlayView];

When the data is loaded from the API, I hide the overlay view and the activity indicator:

当从 API 加载数据时,我隐藏了叠加视图和活动指示器:

[self.activityIndicator removeFromSuperview];
[self.overlayView removeFromSuperview];

It is working fine but the problem is the overlay view is not aligned properly, it has a top margin and it is not looking good. Here's how it looks:

它工作正常,但问题是叠加视图没有正确对齐,它有一个上边距并且看起来不太好。这是它的外观:

enter image description here

在此处输入图片说明

回答by Paul.s

Dont't use

不要使用

self.overlayView.frame = self.tableView.frame;

instead use

而是使用

self.overlayView.frame = self.tableView.bounds;

It looks like your tableView has origin.y = 20.0f;

看起来你的 tableView 有 origin.y = 20.0f;



Other notes

其他注意事项

  • The designated initializer for UIViewis initWithFrame:so you should be using that.

  • You can simply this

    self.activityIndicator.center = CGPointMake(frame.size.width/2, frame.size.height/2);
    

    to

    self.activityIndicator.center = self.overlayView.center;
    
  • UIView的指定初始值设定项是initWithFrame:您应该使用的。

  • 你可以简单地这个

    self.activityIndicator.center = CGPointMake(frame.size.width/2, frame.size.height/2);
    

    self.activityIndicator.center = self.overlayView.center;