我将UILabel和UIActivityIndicatorView作为子视图添加到tableview中作为加载指示符。我希望这些项目位于可见的表格视图的中间,如果用户滚动,则保留在中心位置,以便用户始终可以看到项目。我创建它们的代码是:UILabel和UIActivityIndicatorView居中在UITableView的可见区域
if (!spinner) {
spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[spinner startAnimating];
loadingLabel = [[UILabel alloc] initWithFrame:CGRectZero];
loadingLabel.font = [UIFont systemFontOfSize:20];
loadingLabel.textColor = [UIColor grayColor];
loadingLabel.text = @"Loading...";
[loadingLabel sizeToFit];
static CGFloat bufferWidth = 8.0;
CGFloat totalWidth = spinner.frame.size.width + bufferWidth + loadingLabel.frame.size.width;
CGRect spinnerFrame = spinner.frame;
spinnerFrame.origin.x = (self.tableView.bounds.size.width - totalWidth)/2.0;
spinnerFrame.origin.y = (self.tableView.bounds.size.height - spinnerFrame.size.height)/2.0;
spinner.frame = spinnerFrame;
spinner.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
[self.tableView addSubview:spinner];
CGRect labelFrame = loadingLabel.frame;
labelFrame.origin.x = (self.tableView.bounds.size.width - totalWidth)/2.0 + spinnerFrame.size.width + bufferWidth;
labelFrame.origin.y = (self.tableView.bounds.size.height - labelFrame.size.height)/2.0;
loadingLabel.frame = labelFrame;
loadingLabel.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
[self.tableView addSubview:loadingLabel];
}
如何在表格视图滚动时使其重新定位?
编辑
我将如何计算重新定位的微调,以中间的大小?我会设置什么属性?
它是UIViewController还是UITableViewController? – Emon 2011-12-20 04:17:20
这是一个UITableViewController,其中上面的代码正在运行,并且表视图是标签/活动指示器应显示的位置。 – charleyh 2011-12-20 04:19:33