UILabel 和 UIActivityIndi​​catorView 位于 UITableView 可见区域的中心

发布于 2024-12-21 21:35:56 字数 1898 浏览 0 评论 0原文

我将 UILabel 和 UIActivityIndi​​catorView 作为子视图添加到表视图中作为加载指示器。我希望这些项目位于表视图可见区域的中间,并且在用户滚动时保持在中心,因此用户将始终看到这些项目。我创建它们的代码是:

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];
    }

当表视图滚动时,如何使其重新居中?

编辑

我将如何计算将微调器重新定位到中间的尺寸?我会设置什么属性?

I am adding a UILabel and UIActivityIndicatorView as a subview to a tableview as a loading indicator. I want these items to be in the middle of the visible are of the table view, and remain in the center if the user scrolled, so the user will always see the items. My code to create them is:

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];
    }

How can I make it recenter when the table view scrolls?

EDIT

How would I calculate the sizes to reposition the spinner to the middle? What property would I set?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

殤城〤 2024-12-28 21:35:56
  1. 将您的视图添加到表格视图的超级视图(如果可能;UITableViewController使这成为不可能)

  2. 将您的视图添加到表格视图并将其重新定位在 -scrollViewDidScroll: 委托方法中(UITableViewDelegate是一个UIScrollViewDelegate 的子协议)

编辑

用于维护视图的中心

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{ 

CGSize adSize = [awView actualAdSize]; 

CGRect newFrame = awView.frame; newFrame.origin.x = (self.view.bounds.size.width - adSize.width)/2; 

newFrame.origin.y = (self.tableView.contentOffset.y - adSize.height/2) -21; 

awView.frame = newFrame; 
}
  1. Add your view to the superview of the table view (if possible; UITableViewControllermakes this impossible)

  2. Add your view to the table view and reposition it in the -scrollViewDidScroll:delegate method (UITableViewDelegateis a sub-protocol of UIScrollViewDelegate)

EDIT:

For maintaining center of the view

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{ 

CGSize adSize = [awView actualAdSize]; 

CGRect newFrame = awView.frame; newFrame.origin.x = (self.view.bounds.size.width - adSize.width)/2; 

newFrame.origin.y = (self.tableView.contentOffset.y - adSize.height/2) -21; 

awView.frame = newFrame; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文