Mer*_*ott 142 ios uicollectionview uirefreshcontrol
我试图将添加UIRefreshControl到UICollectionView,但问题是,刷新控制不会出现,除非集合视图填补了其父容器的高度.换句话说,除非集合视图足够长以至于需要滚动,否则无法向下拉以显示刷新控件视图.一旦集合超过其父容器的高度,它就会被拉下并显示刷新视图.
我已经UICollectionView在主视图内部设置了一个快速iOS项目,并在集合视图中添加了一个插座,以便我可以添加UIRefreshControl它viewDidLoad.还有一个带有重用标识符的原型单元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)
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)
| 归档时间: |
|
| 查看次数: |
58823 次 |
| 最近记录: |