简体   繁体   English

iOS7 UIRefreshControl更改contentInset

[英]iOS7 UIRefreshControl changes contentInset

I have a UINavigationController in my app. 我的应用程序中有一个UINavigationController The UINavigationBar is set to opaque and all the scroll views do not overlap underneath the bar. UINavigationBar设置为不透明,并且所有滚动视图不会在条形图下方重叠。

In one view I have a UITableView . 在一个视图中,我有一个UITableView The frame of the UITableView is (0 0; 320 504) on my iPhone 5. ie the height is 568 - 64 (the height of the nav bar and status bar). UITableView的框架在我的iPhone 5上是(0 0; 320 504) 。即高度为568 - 64(导航栏和状态栏的高度)。

The contentInset of the UITableView is (0, 0, 0, 0) . UITableViewcontentInset(0, 0, 0, 0) contentInset (0, 0, 0, 0) When the table view first loads the contentOffset is (0, 0) . 当表视图首次加载时, contentOffset(0, 0)

This is fine. 这可以。 Works brilliantly. 工作出色。

I added a UIRefreshControl to the table view. 我在表视图中添加了一个UIRefreshControl This works a couple of times but then after a few times of doing pull to refresh then the content at the top gets "stuck" under the nav bar. 这可以工作几次,但是经过几次拉动刷新之后,顶部的内容会在导航栏下“卡住”。

When this happens I inspect the contentInset and it is now (-60, 0, 0, 0) . 当发生这种情况时,我检查contentInset ,它现在是(-60, 0, 0, 0) contentInset (-60, 0, 0, 0)

Is there any way to stop the UIRefreshControl from changing the contentInset ? 有没有办法阻止UIRefreshControl更改contentInset

This is probably the reason why UIRefreshControl is currently only supported on UITableViewController , rather than by addition to any scrollview (which you can get away with, in many cases). 这可能是UIRefreshControl目前仅在UITableViewController受支持的原因,而不是添加到任何scrollview(在许多情况下你可以使用它)。

The refresh control does its magic by tinkering with the content insets of the scrollview - particularly when it ends refreshing. 刷新控件通过修改scrollview的内容插入来实现其魔力 - 特别是当它结束刷新时。 Unfortunately the view controller is also tinkering with the content insets of the scroll view to fit it under the translucent nav and status bars. 不幸的是,视图控制器还在修改滚动视图的内容插入,以使其适合半透明的导航和状态栏。 Fun ensues. 随之而来的乐趣。 Is this also an issue on iOS 6 (or, "good old iOS6" as I called it when dealing with the same issue). 这也是iOS 6上的一个问题(或者,当我处理同样的问题时,我称之为“好旧的iOS6”)。

The quickest solution is probably to add your table view as a child UITableViewController instead of a simple subview. 最快的解决方案可能是将您的表视图添加为子UITableViewController而不是简单的子视图。 I think that UITableViewController manages the insets for you at the end of the refresh. 我认为UITableViewController在刷新结束时为您管理insets。 If that doesn't work, I've got workarounds for this but it will have to wait until I get back in the office. 如果这不起作用,我已经为此做了解决方法,但它必须等到我回到办公室。

I will add this answer here in case any one has problems with UIRefreshControl by changing the control properties (attributed title, tint, etc...): 我将在这里添加这个答案,以防任何人通过更改控件属性(属性标题,色调等等)与UIRefreshControl有问题:

Don't mess with the UIRefreshControl on -viewDidLoad: , use -viewDidAppear: instead. 不要乱用UIRefreshControl上的-viewDidLoad: ,请使用-viewDidAppear:代替。

Reset your table view contentInset. 重置表视图contentInset。

-(void)pullToRefresh
{
    [self.tableView reloadData];
    [self.refreshControl endRefreshing];
    [self.tableView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
}

You need override setContentInset: in you UICollectionView 您需要在UICollectionView中覆盖setContentInset :.

- (void)setContentInset:(UIEdgeInsets)contentInset {
  if (self.tracking) {
    CGFloat difference = contentInset.top - self.contentInset.top;
    CGPoint translation = [self.panGestureRecognizer translationInView:self];
    translation.y -= difference * 3.0 / 2.0;
    [self.panGestureRecognizer setTranslation:translation inView:self];
  }
  [super setContentInset:contentInset];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM