Problems with UIPageControl, Objective-C

I have been working on implementing a UIPageViewController for some time now. I have overcome many problems that I have been able to solve on my own and with some additional help. Now I am stuck with the last part and that is UIPageControl.

I am having two problems that I need help with:

Problem 1: Is there an easy way to resize the dots for the PageControl?

Problem 2: This is how it is built:

enter image description here

I know this is difficult to understand, but I will explain them starting from the top left corner and going to the right. The first VC is just a navigation controller pointing to a UITableViewController.

on the second line, the first VC is the datasource delegate for the UIPageVIewController. Next to it is the PageViewController, the two UITableViews next to it are the "pages" that are in the UIPageViewController. And the last one is PageContentViewController.

So, I added this code in viewDidLoad to the VC datasource, which means the first VC in the second line:

self.navigationController.delegate = self;
CGSize navBarSize = self.navigationController.navigationBar.bounds.size;
CGPoint origin = CGPointMake( navBarSize.width/2, navBarSize.height/2 );
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(origin.x, origin.y+16,
                                                                   0, 0)]; //Here added 45 to Y and it did the trick



self.pageControl.pageIndicatorTintColor = navbarColor;
self.pageControl.currentPageIndicatorTintColor = [UIColor lightGrayColor];

[self.pageControl setNumberOfPages:2];

      

and

- (void)navigationController:(UINavigationController *)navigationController     willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    int index = [self.navigationController.viewControllers indexOfObject:viewController];
    self.pageControl.currentPage = index;  }

      

Which gives me this result:

enter image description here

Which is ideal, I can scroll to the right and left and the correct indicator is displayed in the PageControl, etc. However, when I leave the value "UIPageViewController" when I click. The PageController still appears in the first VC in the first snapshot. Why is it flagged and not removed and how can I solve this?

If you need additional codes / pictures that would make it easier to understand, just tell me. Thank!

EDIT

Ok, so I solved the first issue thanks to the comment about removing it in viewWillDissapear. I used this code in the ViewController:

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];

    [self.pageControl removeFromSuperview];

}

      

Now the PageControl parameter only shows where it should be BUT there is one problem I forgot to mention. After clicking the Back button, the Control page didn't go through, which is great. However, when I switch to another UITabBarItem and then back again, the application crashes. This happened before and after the fix. This is how it looks:

enter image description here

Removing the code I used to implement the PageController fixes the problem:

self.navigationController.delegate = self;
CGSize navBarSize = self.navigationController.navigationBar.bounds.size;
CGPoint origin = CGPointMake( navBarSize.width/2, navBarSize.height/2 );
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(origin.x, origin.y+16,
                                                                   0, 0)]; //Here added 45 to Y and it did the trick



self.pageControl.pageIndicatorTintColor = navbarColor;
self.pageControl.currentPageIndicatorTintColor = [UIColor lightGrayColor];

[self.pageControl setNumberOfPages:2];

      

+3


source to share





All Articles