以编程方式为UITableViewController设置UIActivityIndi​​catorView

时间:2012-03-23 15:21:11

标签: iphone ios uitableview

我有一个常规的UITableViewController和一个UITableView作为唯一的视图,除了表格视图外,我想要一个UIActivittyIndicatorView

所以我需要一个像这样的视图结构:

view (UIView):
  tableView
  activityIndicatorView

没有InterfaceBuilder,最简洁的方法是什么?我想我需要覆盖loadView:方法,但到目前为止我还没有成功。

1 个答案:

答案 0 :(得分:28)

ARC和iOS 5.0+的更新(我认为旧版本需要删除,因为我们有新的更好的API:)):( / p>

添加到UIViewController子类的标题.h文件中:

 @property (nonatomic, weak) UIActivityIndicator *activityIndicator;

覆盖UIViewController子类的.m文件中的方法:

- (void)loadView {
    [super loadView];
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    // If you need custom color, use color property
    // activityIndicator.color = yourDesirableColor;
    [self.view addSubview:activityIndicator];
    [activityIndicator startAnimating];
    self.activityIndicator = activityIndicator;
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    CGSize viewBounds = self.view.bounds;
    self.activityIndicator.center = CGPointMake(CGRectGetMidX(viewBounds), CGRectGetMidY(viewBounds));
} 

=============================================== ==============

非ARC版本,iOS< 5.0:

您应该覆盖方法

-(void)loadView {
    [super loadView];
    self.activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
    [self.view addSubview:self.activityIndicator];
    self.activityIndicator.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
    [self.activityIndicator startAnimating];
}

另外,添加

@property (nonatomic, assign) UIActivityIndicatorView *activityIndicator;

在头文件

@synthesize activityIndicator;

到.m文件