【问题标题】:UIRefreshControl not centred with contentInset setUIRefreshControl 不以 contentInset 集为中心
【发布时间】:2018-06-20 15:54:18
【问题描述】:

我在集合视图中添加了一个刷新控件。一切正常,但是当我设置 collectionView.contentInset = UIEdgeInsetsMake(0, 20, 20, 20) 时,刷新控件的宽度保持不变,但移动了 20px,因此不在视图中居中。控件应减少 40px 才能正确。

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 中),但使用 autoresizingMask 无济于事。

有什么想法吗?我想我需要将它放在容器视图中......但不需要!

【问题讨论】:

  • 容器视图不起作用,因为 UIRefreshControl 断言如果未添加到滚动视图!

标签: ios objective-c uicollectionview uirefreshcontrol


【解决方案1】:

好的,通过不设置 collectionView.contentInset 解决了这个问题。相反,我设置了 flowLayout

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

【讨论】:

  • 节省了很多时间。
【解决方案2】:

接受的答案不符合我的需求,因为我有一个复杂的 collectionView,其中包含多种类型的单元格,并且每个单元格都有特定的部分插入,因此我不想深入研究并使用此解决方案增加复杂性。

对我有用的方法很简单。将UIRefreshControl 添加到collectionView 的backgroundView 中:

collectionView.backgroundView = viewModel.refreshControl

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

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

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多