ios UICollectionView 上的 UIRefreshControl 仅在集合填充容器的高度时才有效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14678727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIRefreshControl on UICollectionView only works if the collection fills the height of the container
提问by Merott
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.
我正在尝试将 a 添加UIRefreshControl
到 a UICollectionView
,但问题是刷新控件不会出现,除非集合视图填满其父容器的高度。换句话说,除非集合视图足够长需要滚动,否则无法下拉以显示刷新控制视图。一旦集合超过其父容器的高度,它就会被下拉并显示刷新视图。
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 UIRefreshControl
to it in viewDidLoad
. There is also a prototype cell with the reuse identifier cCell
我已经UICollectionView
在主视图内部设置了一个快速的 iOS 项目,并带有一个集合视图的出口,以便我可以UIRefreshControl
在viewDidLoad
. 还有一个带有重用标识符的原型单元格cCell
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?
这是控制器中的所有代码,它很好地演示了这个问题。在这段代码中,我将单元格的高度设置为 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);
}
回答by Larry
Try this:
尝试这个:
self.collectionView.alwaysBounceVertical = YES;
self.collectionView.alwaysBounceVertical = YES;
Complete code for a UIRefreshControl
一个完整的代码 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;
回答by avdyushin
回答by Andrew Schreiber
Larry's answer in swift:
拉里迅速回答:
let refreshControl = UIRefreshControl()
refreshControl.tintColor = UIColor.blueColor()
refreshControl.addTarget(self, action: "refresh", forControlEvents: .ValueChanged)
collectionView.addSubview(refreshControl)
collectionView.alwaysBounceVertical = true
Swift 3:
斯威夫特 3:
let refreshControl = UIRefreshControl()
refreshControl.tintColor = .blue
refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
collectionView.addSubview(refreshControl)
collectionView.alwaysBounceVertical = true
回答by Nghia Luong
If your collectionview
has a content size big enough to scroll vertically, it's OK, but in your case it's not.
如果您collectionview
的内容大小足以垂直滚动,那没关系,但在您的情况下则不然。
You must enable the property AlwaysBounceVertical
, so you could set self.collectionView.alwaysBounceVertical = YES;
您必须启用该属性AlwaysBounceVertical
,以便您可以设置self.collectionView.alwaysBounceVertical = YES;
回答by Saif
I too was facing the same issue, I was not able to use the UIRefreshControl
until the UICollectionView
's content size was large enough to scroll vertically,
我也面临同样的问题,UIRefreshControl
直到UICollectionView
的内容大小足以垂直滚动,我才能使用,
Setting the bounces
property of UICollectionView
solved this
设置bounces
属性UICollectionView
解决了这个问题
[self.collectionView setBounces:YES];
[self.collectionView setAlwaysBounceVertical:YES];
回答by Nikolai Ischuk
I'm calling beginRefreshing()
right after viewDidLoad()
, but on some screens it doesn't work. And only collectionView.layoutIfNeeded()
in viewDidLoad()
helped me
我打电话beginRefreshing()
之后viewDidLoad()
,但有些画面中这是行不通的。只有collectionView.layoutIfNeeded()
在viewDidLoad()
帮助我
回答by Asad Jamil
You must check in api call if collection view is in refreshing state then end refreshing to dismiss refreshing control.
如果集合视图处于刷新状态,则必须检查 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()
}