UITableView 并将 Activity Indicator 视图居中

UITableView and centering the Activity Indicator view

我有一个从 Parse.com 获取数据的 UITableView。在执行此操作时,我想在 table 居中上方显示一个 Activity 指示器。我的代码是:

        UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        indicator.center=self.view.center;
        [self.view addSubview:indicator];
        [indicator startAnimating];

但是它不是很中心:

抱歉图片太大了!我尝试将指标添加到 table superView,但没有任何区别。这一切有点菜鸟。有人可以指点我可能做错了什么吗?

没有没有。你不需要这些。您只需执行以下操作:

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

如果您想关闭它,请执行以下操作:

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

不必担心将指示器添加为子视图,或者它的位置或大小。它是状态栏的一部分,Apple 会处理它,您无法更改它size/colour/etc...您可以打开和关闭它。

更新

哦抱歉,我以为你在谈论 UIStatusBar activity 指标。在回答您的问题时,为了使 UIActivityIndi​​catorView 居中,只需执行以下操作:

UIActivityIndicatorView *spinner = [[UIActivityIndicatorVIew alloc] 
         initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
CGRect frame = spinner.frame;
frame.origin.x = (self.view.frame.size.width / 2 - frame.size.width / 2);
frame.origin.y = (self.view.frame.size.height / 2 - frame.size.height / 2);
spinner.frame = frame;
[view addSubview:spinner];
[spinner startAnimating];

试试这个!

UIActivityIndicatorView * indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.frame = CGRectMake(65, 20, indicator.bounds.size.width, indicator.bounds.size.height);
[UIApplication sharedApplication].networkActivityIndicatorVisible = TRUE;
[self.view addSubview:indicator];
[indicator startAnimating];

您可以使用 UITableview 的背景视图来显示 ActivityIndi​​cator。

lazy var activityIndicatorBase : UIActivityIndicatorView = {
        let activityIndicator = UIActivityIndicatorView()
        activityIndicator.frame = CGRect(x:0.0, y:0.0, width:60.0, height:60.0);
        activityIndicator.hidesWhenStopped = true
        activityIndicator.activityIndicatorViewStyle = .gray
        activityIndicator.color = UIColor.orange
        activityIndicator.startAnimating()
        return activityIndicator
    }()

显示指标

self.tblMessage.backgroundView = self.activityIndicatorBase

隐藏指标

self.tblMessage.backgroundView = nil