UIActivityIndi​​catorView浮动在UITableView上方

时间:2015-04-18 08:08:19

标签: ios uitableview uiactivityindicatorview

我有一个UIActivityIndi​​catorView用于刷新我桌面上的内容,它在视图中居于:

//Start activity indicator
        UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        indicator.center=self.view.center;
        CGRect frame = indicator.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)-self.navigationController.navigationBar.frame.size.height-20;
        indicator.frame = frame;
        [self.view addSubview:indicator];
        [indicator startAnimating];

然而,目前它与桌子一起滚动,是否可以让它浮动在桌子上方,以便在用户滚动时保持居中?我尝试将它添加到表格superview但是没有用。任何指针都会非常感激!感谢

4 个答案:

答案 0 :(得分:4)

您可以添加任何类型的视图"在顶部"任何tableview,导航控制器或任何其他只是通过子视图添加创建的视图非常类似于您的代码,但具有正确的根视图。只需让您清楚地识别所需的根视图。

例如,以下是我在其中一个应用中在导航控制器上实现子视图的方法:

...
[self.navigationController.view addSubview:indicator];
...

答案 1 :(得分:3)

听起来你使用的是UITableViewController,如果是这种情况,我建议改用UIViewController。这样您就可以在UIView的顶部添加UITableView以保留UIActivityIndicatorView您还可以轻松地将其放在故事板中并将其放置在您想要的位置并且它不会随着桌面滚动而移动,因为它是它自己的视图。作为一般规则,我尽量不使用UITableViewController因为它会限制你在这种情况下使用。

答案 2 :(得分:1)

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] 
   initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  spinner.center = CGPointMake(160, 240);
  spinner.hidesWhenStopped = YES;
  [self.view addSubview:spinner];
  [spinner startAnimating

];

答案 3 :(得分:0)

您可以将指标添加到tableView的超级视图中,但是,如果它没有成功,您还可以在应用程序的UIWindow上添加活动指示器。以下是代码:

UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow]; // this gives you the current window of the application.
[keyWindow addSubview:indicator]; // add the indicator on the window.

完成工作后,使用

从窗口中删除活动指示器
[indicator removeFromSuperView];
indicator = nil;