UIRefreshControl 设置 contentInset 后没有居中显示

3

我在集合视图中添加了一个刷新控件,一切都工作正常,但是当我设置collectionView.contentInset = UIEdgeInsetsMake(0, 20, 20, 20)时,刷新控件的宽度保持不变,但向右移动了20像素,因此没有居中显示。为了使其正确,应将控件减小40像素。

self.collectionView = [[UICollectionView alloc] initWithFrame:rect collectionViewLayout:flowLayout];
self.collectionView.contentInset = UIEdgeInsetsMake(0, 20, 20, 20)
UIRefreshControl* pullDownRefresh = [[UIRefreshControl alloc] init];
[pullDownRefresh addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
[self.collectionView addSubview:pullDownRefresh];

我已经尝试过在添加视图后手动设置宽度(也在viewWillLayoutSubviews中尝试),并且调整了自动调整大小的掩码,但都没有效果。

有什么想法吗?我猜我需要把它放在容器视图中……但不应该需要这样做!


容器视图不起作用,因为UIRefreshControl会断言如果没有添加到滚动视图中! - Nick Hingston
2个回答

13

好的,通过不设置collectionView.contentInset来解决了问题。相反,我在flowLayout上进行了设置。

即:

flowLayout.sectionInset = UIEdgeInsetsMake(0, 20, 20, 20);

节省了很多时间。 - Nikhil Manapure

0

接受的答案并不符合我的需求,因为我有一个复杂的collectionView,其中包含多种类型的单元格,每个单元格都有特定的节间距,所以我不想通过这种解决方案增加复杂性。

对我有效的方法非常简单。将UIRefreshControl添加到collectionViewbackgroundView中:

collectionView.backgroundView = viewModel.refreshControl

当刷新完成后,只需执行以下操作:

DispatchQueue.main.async {
    self.viewModel.refreshControl?.endRefreshing()
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接