UICollectionView上的UIRefreshControl仅在集合填充容器高度时才有效

Mer*_*ott 142 ios uicollectionview uirefreshcontrol

我试图将添加UIRefreshControlUICollectionView,但问题是,刷新控制不会出现,除非集合视图填补了其父容器的高度.换句话说,除非集合视图足够长以至于需要滚动,否则无法向下拉以显示刷新控件视图.一旦集合超过其父容器的高度,它就会被拉下并显示刷新视图.

我已经UICollectionView在主视图内部设置了一个快速iOS项目,并在集合视图中添加了一个插座,以便我可以添加UIRefreshControlviewDidLoad.还有一个带有重用标识符的原型单元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);
}
Run Code Online (Sandbox Code Playgroud)

Lar*_*rry 391

试试这个:

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;
Run Code Online (Sandbox Code Playgroud)

  • 是的没问题.显而易见,我是新手吧.无论如何,当我看到你的帖子时,我被困在同样的情况.当我找到解决方案时,我应该肯定分享它.干杯 (7认同)
  • 嗯,胡安,你可能已经找到了问题的答案.但是,为了在刷新后将刷新控制返回到正常状态,必须调用`[refreshControl endRefreshing]`. (5认同)
  • 遗憾的是,不再支持此功能.UIRefreshControl只能与UITableViewController一起使用,现在严格执行. (2认同)
  • 在iOS 10中,`UIScrollView`(`UICollectionView`的超级视图)现在有一个`refreshControl`属性https://developer.apple.com/videos/play/wwdc2016/219/?time=2033 (2认同)

avd*_*hin 29

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

在此输入图像描述


And*_*ber 21

拉里迅速回答:

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

斯威夫特3:

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