Pull-to-refresh in UICollectionViewController

后端 未结 5 983
闹比i
闹比i 2021-01-29 19:06

I want to implement pull-down-to-refresh in a UICollectionViewController under iOS 6. This was easy to achieve with a UITableViewController, like so:

5条回答
  •  一生所求
    2021-01-29 19:43

    The answers to both (1) and (2) are yes.

    Simply add a UIRefreshControl instance as a subview of .collectionView and it just works.

    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(startRefresh:)
        forControlEvents:UIControlEventValueChanged];
    [self.collectionView addSubview:refreshControl];
    

    That's it! I wish this had been mentioned in the documentation somewhere, even though sometimes a simple experiment does the trick.

    EDIT: this solution won't work if the collection is not big enough to have an active scrollbar. If you add this statement,

    self.collectionView.alwaysBounceVertical = YES;
    

    then everything works perfectly. This fix taken from another post on the same topic (referenced in a comment in the other posted answer).

提交回复
热议问题