3

I have a UICollectionView inside a UIViewController. Now, I need to add a UIPageViewController inside the custom UICollectionViewCell that i created. How can i do this ?

Here's my approach.

UIVIewController.h class

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

        PViewCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

        [cell.pageViewController addTarget:self action:@selector(changed:) forControlEvents:UIControlEventTouchUpInside];

        return cell;

}

PViewCollectionViewCell.h Class

#import <UIKit/UIKit.h>



@interface PViewCollectionViewCell : UICollectionViewCell <UIPageViewControllerDataSource>

@property (strong, nonatomic) IBOutlet UIImageView *pageBannerImageView;

@property (strong, nonatomic) IBOutlet UIPageControl *pageViewController;

- (IBAction)pageVIewChanged:(id)sender;



@end
Illep
  • 16,375
  • 46
  • 171
  • 302

1 Answers1

0

You are asking "How to implement UIPageViewController" and giving code example with UIPageControl. It is a completely different thing. I would never recommend implementing UIPageViewController on UICollectionViewCell, because it might kill your performance. The best approach would be UIScrollView + UIPageControl.

On your UICollectionViewCell add UIScrollViewDelegate:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageControl.currentPage = page;
}

Also, enable paging on your scroll using:

self.scrollView.pagingEnabled = YES;

Here you can find many answers which are well described. I would say that it does not really matter where you implement pagination - on UIView or UICollectionViewCell. The only thing you should do is make sure that UICollectionView scroll gesture does not cancel your UICollectionView pagination scroll gesture.

Please, do not duplicate questions. First have a look on existing posts and create your own one only if you did not find anything.

Community
  • 1
  • 1
gontovnik
  • 690
  • 5
  • 9