Search code examples
iphoneiosuinavigationcontrolleruiscrollviewuipagecontrol

iOS Paging Controls (Navigation)


Is there a way that I can use those paging dots independently in my app,

add them to my UIView/NIB and call the functions on it to set total number of dots or current page/dot.

Basically I've UIViewImage in a nib file, i'm displaying images (names taken off an array) on swipe gestures and want to show those paging dots on the bottom for the navigation information.


Solution

  • UIPageControl has it's total page & current page/dot just as you described, let's say pageControl is a instance of UIPageControl, then you can initiate the numberOfPages & currentPage as below:

    pageControl.numberOfPages = [images count];
    pageControl.currentPage = 0;
    

    Also, you can add action for it when the pageControl's dots was tapped. changePage: method here is just a example:

    [pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    

    You can add your images to a UIScrollView and use its delegate method: scrollViewDidScroll::

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

    It'll change the pageControl's current dot when your swiped to a new image.