UICollectionView 上的 UIRefreshControl 仅在集合填充容器的高度时才有效 [英] UIRefreshControl on UICollectionView only works if the collection fills the height of the container

查看:14
本文介绍了UICollectionView 上的 UIRefreshControl 仅在集合填充容器的高度时才有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to add a UIRefreshControl to a UICollectionView, but the problem is that the refresh control does not appear unless the collection view fills up the height of its parent container. In other words, unless the collection view is long enough to require scrolling, it cannot be pulled down to reveal the refresh control view. As soon as the collection exceeds the height of its parent container, it is pulled down and reveals the refresh view.

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

I have set up a quick iOS project with just a UICollectionView inside the main view, with an outlet to the collection view so that I can add the UIRefreshControlto it in viewDidLoad. There is also a prototype cell with the reuse identifier cCell

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

This is all the code in the controller, and it demonstrates the issue pretty well. In this code I set the height of the cell to 100, which isn't enough to fill the display, and therefore the view cannot be pulled and the refresh control won't show. Set it to something higher to fill the display, then it works. Any ideas?

@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);
}

推荐答案

试试这个:

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;

这篇关于UICollectionView 上的 UIRefreshControl 仅在集合填充容器的高度时才有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆