Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we customize the page indicator in UIPageViewController?

Now it's white dots with black background. What about if I want it to be black dots with white backgrounds?

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(6_0) {     return _imageArrays.count; }// The number of items reflected in the page indicator. - (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(6_0) {     return self.intCurrentIndex; }// The selected item reflected in the page indicator. 
like image 302
Septiadi Agus Avatar asked Jul 04 '13 09:07

Septiadi Agus


2 Answers

You can use UIAppearance to change the color of UIPageControl. Try this in your AppDelegate's didFinishLaunchingWithOptions function.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     UIPageControl *pageControl = [UIPageControl appearance];     pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];     pageControl.currentPageIndicatorTintColor = [UIColor blackColor];     pageControl.backgroundColor = [UIColor blueColor];      return YES; } 

EDIT:

To apply style only to a particular view controller, use appearanceWhenContainedIn instead, as following:

UIPageControl *pageControl = [UIPageControl appearanceWhenContainedIn:[MyViewController class], nil]; 

Now, only UIPageControl objects contained in the MyViewController are going to adapt this style.

Thanks Mike & Shingoo!

EDIT: If you see black background around UIPageControl at the bottom of your screen, it is due to the background color of your UIPageViewController not UIPageControl. You can change this color as following:

- (void)viewDidLoad {     [super viewDidLoad];     self.view.backgroundColor = [UIColor blueColor]; //Set it to whatever you like } 
like image 172
Yas Tabasam Avatar answered Oct 17 '22 11:10

Yas Tabasam


I don't believe that you can manipulate the UIPageViewController's page control. My solution:

I have a "root" UIViewController that is UIPageViewControllerDelegate and UIPageViewControllerDataSource.

On this root view controller, I have @property (strong, nonatomic) IBOutlet UIPageControl *pageControl. In the corresponding storyboard nib, I add a UIPageControl, position it, and check "Hides for Single Page". I can also change the colors, if I wish.

Then, I add the following in the root view controller's viewDidLoad: self.pageControl.numberOfPages = [self.features count]

My root view controller also has @property (strong, nonatomic) UIPageViewController *pageViewController. And in the implementation:

self.pageViewController = [[UIPageViewController alloc]           initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll              navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal                            options:nil];   self.pageViewController.delegate = self; DataViewController *startingViewController = [self viewControllerAtIndex:0 storyboard:self.storyboard]; NSArray *viewControllers = @[startingViewController]; [self.pageViewController setViewControllers:viewControllers                                   direction:UIPageViewControllerNavigationDirectionForward                                    animated:NO                                  completion:NULL]; self.pageViewController.dataSource = self;       [self addChildViewController:self.pageViewController]; [self.view addSubview:self.pageViewController.view]; self.pageViewController.view.frame = CGRectMake(0.0, 0.0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height + 10.0); [self.pageViewController didMoveToParentViewController:self]; self.view.gestureRecognizers = self.pageViewController.gestureRecognizers; 

(SIDE NOTE: That line that sets the frame makes the height of the UIPageViewController's view exceed the screen size so that the native page control is no longer visible. My app is portrait only, iPhone only, so I got off a bit easy here. If you need to handle rotations, you'll have to find a way to keep that native page control offscreen. I tried using auto layout, but UIPageViewController creates a set of magic views that have a bunch of autolayout mask constraints that I couldn't find a way to override.)

Anyway...then I add an extra UIPageViewController delegate method to change my new, non-native UIPageControl to the currently-selected page:

- (void)pageViewController:(UIPageViewController *)viewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {   if (!completed){return;}    // Find index of current page   DataViewController *currentViewController = (DataViewController *)[self.pageViewController.viewControllers lastObject];   NSUInteger indexOfCurrentPage = [self indexOfViewController:currentViewController];   self.pageControl.currentPage = indexOfCurrentPage; } 

Not as pretty as I would like, but Apple's API for this class doesn't exactly lend itself to elegance.

like image 44
sirvine Avatar answered Oct 17 '22 11:10

sirvine