UITableView与UIRefreshControl在半透明状态栏下 [英] UITableView with UIRefreshControl under a semi-transparent status bar

查看:110
本文介绍了UITableView与UIRefreshControl在半透明状态栏下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个UITableView,我想在我的半透明黑色状态栏下滚动。在我的XIB中,我只是将表格视图的y位置设置为-20,它看起来都很好。

I've created a UITableView which I want to scroll underneath my semi-transparent black status bar. In my XIB, I just set the table view's y position to -20 and it all looks fine.

现在,我刚刚添加了一个刷新的iOS6然而,UIRefreshControl可以工作,由于-20°位置,它从状态栏后面拖动。我希望它伸展到状态位于状态栏之下而不是落后。

Now, I've just added a pull-to-refresh iOS6 UIRefreshControl which works however, because of the -20 y position, it drags from behind the status bar. I'd like it's "stretched to" position to be under the status bar rather than behind.

为什么它会搞乱,但似乎没有任何差异改变它的框架和tableview的内容插入等都没有什么区别。

It makes sense why it's messing up but there doesn't seem to be any difference changing it's frame and the tableview's content insets etc don't make a difference.

文档建议一旦设置了refreshControl,UITableViewController会处理它从那时起的位置。

The docs suggest that once the refreshControl has been set, the UITableViewController takes care of it's position from then on.

任何想法?

推荐答案

你可以继承 UIRefreshControl 并实现 layoutSubviews ,如下所示:

You can subclass the UIRefreshControl and implement layoutSubviews like so:

@implementation RefreshControl {
    CGFloat topContentInset;
    BOOL topContentInsetSaved;
}

- (void)layoutSubviews {
    [super layoutSubviews];

    // getting containing scrollView
    UIScrollView *scrollView = (UIScrollView *)self.superview;

    // saving present top contentInset, because it can be changed by refresh control
    if (!topContentInsetSaved) {
        topContentInset = scrollView.contentInset.top;
        topContentInsetSaved = YES;
    }

    // saving own frame, that will be modified
    CGRect newFrame = self.frame;

    // if refresh control is fully or partially behind UINavigationBar
    if (scrollView.contentOffset.y + topContentInset > -newFrame.size.height) {
        // moving it with the rest of the content
        newFrame.origin.y = -newFrame.size.height;

    // if refresh control fully appeared
    } else {
        // keeping it at the same place
        newFrame.origin.y = scrollView.contentOffset.y + topContentInset;
    }

    // applying new frame to the refresh control
    self.frame = newFrame;
}

需要tableView的 contentInset 考虑到了,但您可以将 topContentInset 变量更改为您需要的任何值,它将处理其余的值。

It takes tableView's contentInset into account, but you can change topContentInset variable to whatever value you need and it will handle the rest.

我希望代码有足够的文档来理解它是如何工作的。

I hope the code is documented enough to understand how it works.

这篇关于UITableView与UIRefreshControl在半透明状态栏下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆