Search code examples
iosuicollectionviewuipagecontrol

UIPageControl set numberOfPages with UICollectionView page?


I'm trying to add an UIPageControl and UICollectionView. My collection is enable paging, and now I want my page control numberOfPages is the collection view number of page.

I try to set it like this:

self.pageControl.numberOfPages = self.gridMenuCollection.frame.size.width / self.gridMenuCollection.contentSize.width;

but it's not work. If using my data array for numberOfPage it return alot of dots and I don't want to do it. Can anyone help?


Solution

  • You are doing the devision the other way around. Should be:

    self.pageControl.numberOfPages = self.gridMenuCollection.contentSize.width / self.gridMenuCollection.frame.size.width;

    And also use ceiling to show the correct amount of dots. like this e.g. content width is 1200 frame width is 500 content / frame = 2.4 ceil(content / frame) = 3

    And you want to show 3 dots, because the elements will fit only to 3.