Search code examples
iosuiscrollviewuipagecontrol

UIPageControl Disappearing


I have been working on this for quite a while now and i just cant figure it out.

My page control keeps disappearing.

I originally had my code so that it worked but it was displaying the pagecontrol in-between scrolling through views.

How ever i have changed this now and it displays when scrolling forward on two of the views and the others it doesn't work for the page control doesn't even display. When you scroll backwards to the first view the page control also disappears(including the two it was working on at the begining of running my code.

All of my views have the same settings so I'm not sure why this keeps happening.

Here is a caption of the code that I'm using:

- (void)scrollViewDidScroll:(UIScrollView *)sender {

    if (_pageControlUsed || _rotating) {
        // do nothing - the scroll was initiated from the page control, not the user dragging
        return;
    }

    // Switch the indicator when more than 50% of the previous/next page is visible
    CGFloat pageWidth = self.scrollView.frame.size.width;
    int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    if (self.pageControl.currentPage != page  && page>=0 && page<self.childViewControllers.count) {
        UIViewController *oldViewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage];
        UIViewController *newViewController = [self.childViewControllers objectAtIndex:page];
        [oldViewController viewWillDisappear:YES];
        [newViewController viewWillAppear:YES];
        self.pageControl.currentPage = page;
        [oldViewController viewDidDisappear:YES];
        [newViewController viewDidAppear:YES];
        _page = page;


        CGRect frame = pageControl.frame;
        frame.origin.x = pageWidth;
        pageControl.frame = frame;
    }
}

If any one knows where im going wrong and could help me out that would be brilliant.

Please be aware I'm still fairly new to programming and xcode.


Solution

  • Please add page control in this simple way PageControl Sample

    - (void)scrollViewDidScroll:(UIScrollView *)sender {
        // Update the page when more than 50% of the previous/next page is visible
        CGFloat pageWidth = self.scrollView.frame.size.width;
        int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
        self.pageControl.currentPage = page;
    }
    

    and to change

    - (IBAction)changePage {
        // update the scroll view to the appropriate page
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;
        [self.scrollView scrollRectToVisible:frame animated:YES];
    }
    
    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
        pageControlBeingUsed = NO;
    }
    
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
        pageControlBeingUsed = NO;
    }