0

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?

Bad_Developer
  • 537
  • 5
  • 20

1 Answers1

1

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.

Bence Pattogato
  • 3,752
  • 22
  • 30
  • I tried your solution but the number of pages is nil. I think it's cannot get the contentSize.width & collectionview width because I'm put an breakpoint on it and then print out both `self.gridMenuCollection.contentSize.width` & `self.gridMenuCollection.frame.size.width` then console say: `error: cannot take the address of an rvalue of type 'CGFloat' (aka 'double')`. Using `ceiling` got the same issue. – Bad_Developer Nov 03 '18 at 01:23
  • Oh wait. It's work, but the dots doesn't show up instantly. I put my collection into a UIView as a popup, the first time I show this popup the dots not appear. It's only show up after I dismiss the popup then show popup again. – Bad_Developer Nov 03 '18 at 01:36
  • Fixed by put it in `dispatch_async(dispatch_get_main_queue(), ^{});` It's show instantly. – Bad_Developer Nov 03 '18 at 10:10
  • Yes! Thank you for the answer. – Bad_Developer Nov 05 '18 at 13:41