【问题标题】:UIRefreshControl on UICollectionView only works if the collection fills the height of the containerUICollectionView 上的 UIRefreshControl 仅在集合填充容器的高度时才有效
【发布时间】:2013-01-18 16:18:36
【问题描述】:

我正在尝试将UIRefreshControl 添加到UICollectionView,但问题是刷新控件不会出现,除非集合视图填满其父容器的高度。换句话说,除非集合视图足够长以至于需要滚动,否则它不能被拉下以显示刷新控制视图。一旦集合超过其父容器的高度,它就会被拉下并显示刷新视图。

我已经建立了一个快速的 iOS 项目,在主视图中只有一个 UICollectionView,并带有一个到集合视图的出口,以便我可以在 viewDidLoad 中添加 UIRefreshControl。还有一个原型单元,其重用标识符为cCell

这是控制器中的所有代码,它很好地演示了这个问题。在这段代码中,我将单元格的高度设置为 100,这不足以填充显示,因此无法拉出视图并且不会显示刷新控件。将其设置为更高的值以填充显示,然后它就可以工作了。有什么想法吗?

@interface ViewController () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [self.collectionView addSubview:refreshControl];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    return [collectionView dequeueReusableCellWithReuseIdentifier:@"cCell" forIndexPath:indexPath];
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(self.view.frame.size.width, 100);
}

【问题讨论】:

标签: ios uicollectionview uirefreshcontrol


【解决方案1】:

如果集合视图处于刷新状态,则必须检查 api 调用,然后结束刷新以解除刷新控制。

private let refreshControl = UIRefreshControl()
 refreshControl.tintColor = .white
 refreshControl.addTarget(self, action: #selector(refreshData), for: .valueChanged)
 collectionView.addSubview(refreshControl)
 @objc func refreshData() {
    // API Call
 }
// Disable refresh control if already refreshing 
if refreshControl.isRefreshing {
    refreshControl.endRefreshing()
}

【讨论】:

  • 在我的故事板文件中启用滚动反弹。
【解决方案2】:

我在viewDidLoad() 之后立即调用beginRefreshing(),但在某些屏幕上它不起作用。只有viewDidLoad() 中的collectionView.layoutIfNeeded() 帮助了我

【讨论】:

    【解决方案3】:

    Storyboard/Xib 中的属性/滚动视图/垂直弹跳

    【讨论】:

      【解决方案4】:

      Larry 的回答很快:

          let refreshControl = UIRefreshControl()
          refreshControl.tintColor = UIColor.blueColor()
          refreshControl.addTarget(self, action: "refresh", forControlEvents: .ValueChanged)
          collectionView.addSubview(refreshControl)
          collectionView.alwaysBounceVertical = true
      

      斯威夫特 3:

          let refreshControl = UIRefreshControl()
          refreshControl.tintColor = .blue
          refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
          collectionView.addSubview(refreshControl)
          collectionView.alwaysBounceVertical = true
      

      【讨论】:

      • Const 给出未定义变量的错误。如果有人遇到相同的情况,只需将其替换为 UIColor.whiteColor() 或您喜欢的任何颜色。
      • 对于 Swift 2.2 也可以使用 action: #selector(self.refresh)
      • 现在应该是 collectionView.refreshControl = refreshControl 而不是 collectionView.addSubview(refreshControl)
      【解决方案5】:

      我也面临同样的问题,直到UICollectionView 的内容大小足以垂直滚动,我才能使用UIRefreshControl

      设置UICollectionViewbounces 属性解决了这个问题

      [self.collectionView setBounces:YES];
      [self.collectionView setAlwaysBounceVertical:YES];
      

      【讨论】:

        【解决方案6】:

        如果您的collectionview 的内容大小足以垂直滚动,那没关系,但在您的情况下却不是。

        你必须启用属性AlwaysBounceVertical,所以你可以设置self.collectionView.alwaysBounceVertical = YES;

        【讨论】:

          【解决方案7】:

          试试这个:

          self.collectionView.alwaysBounceVertical = YES;

          UIRefreshControl 的完整代码

          UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
          refreshControl.tintColor = [UIColor grayColor];
          [refreshControl addTarget:self action:@selector(refershControlAction) forControlEvents:UIControlEventValueChanged];
          [self.collectionView addSubview:refreshControl];
          self.collectionView.alwaysBounceVertical = YES;
          

          【讨论】:

          • 太棒了,那行是准确的修复,其余代码无关紧要。在 StackOverflow 上对您来说不是一个糟糕的开始!干杯!
          • 是的,没问题。很明显我是新手哈。无论如何,当我看到你的帖子时,我陷入了同样的境地。当我找到解决方案时,我当然应该分享它。干杯
          • 嗯,胡安,您可能已经找到了问题的答案。但是,为了在刷新后将刷新控件返回到其正常状态,您必须调用 [refreshControl endRefreshing]
          • 很遗憾不再支持此功能。 UIRefreshControl 只能与 UITableViewController 一起使用,现在被严格执行。
          • 在 iOS 10 中,UIScrollViewUICollectionView 的超级视图)现在有一个 refreshControl 属性 developer.apple.com/videos/play/wwdc2016/219/?time=2033
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-08-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-24
          相关资源
          最近更新 更多