我们可以在UIPageViewController中自定义页面指示器吗? [英] Can we customize the page indicator in UIPageViewController?

查看:111
本文介绍了我们可以在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.


推荐答案

我不相信你可以操纵UIPageViewController的页面控制。我的解决方案:

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

我有一个rootUIViewController,它是UIPageViewControllerDelegate和UIPageViewControllerDataSource。

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

在这个根视图上控制器,我有 @property(强,非原子)IBOutlet UIPageControl * pageControl 。在相应的storyboard笔尖中,我添加一个UIPageControl,定位它,并选中Hides for Single Page。如果我愿意,我也可以改变颜色。

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.

然后,我在根视图控制器的viewDidLoad中添加以下内容: self.pageControl.numberOfPages = [self.features count]

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

我的根视图控制器还有 @property(强,非原子)UIPageViewController * pageViewController 。并在实现中:

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;

(侧面注释:设置框架的那条线使UIPageViewController视图的高度超出屏幕尺寸所以本机页面控件不再可见。我的应用程序只是纵向,只有iPhone,所以我在这里稍微有点容易。如果你需要处理旋转,你必须找到一种方法来保持原生页面控制我尝试使用自动布局,但是UIPageViewController会创建一组魔术视图,这些视图有一堆我自己无法覆盖的自动布局遮罩约束。)

(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.)

无论如何......然后我添加了一个额外的UIPageViewController委托方法来将我的新的非本地UIPageControl更改为当前选择的页面:

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;
}

不像我想的那样漂亮,但Apple的这个类的API并不是'完全适合优雅。

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

这篇关于我们可以在UIPageViewController中自定义页面指示器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆