如何做pageViewController点击选择UI?

时间:2015-09-29 07:17:08

标签: ios objective-c uipageviewcontroller

在我的应用程序中,我有一个包含5个页面的pageViewController。我需要页面选择器看起来如下图所示。

enter image description here

目前我使用以下代码选择标签选择器。

-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
    if (!completed) {
        return;
    }
    if(completed)
    {
        [self setTabTextColor:nextIndex];


    }
}

工作 但是在这里我在故事板中创建了5个白色标签选择器(各个文本下方的uiviews)并一次显示一个< /强>

我知道这不是现在这样做的方式。是否有一个自定义控制器来做同样的事情?

如果我做错了,请告诉我正确的方法。

  

在android中,我已经看到了如果我们正在进行pageviewcontroller   滑动,选择器自动动画并自行调整大小   以适应当前的文本宽度。任何人都有任何想法做同样的事情?

1 个答案:

答案 0 :(得分:1)

这很简单,我想这会帮助你开始

1.first create pageViewController

    mPageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:@{UIPageViewControllerOptionInterPageSpacingKey:[NSNumber numberWithFloat:25.0f]}]; //mPageViewController is a property of type UIPageViewController

    [mPageViewController setViewControllers:@[theVC] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    mPageViewController.dataSource = self;

    mPageViewController.delegate = self;

    mPageViewController.view.frame = self.view.frame;

    [self addChildViewController:mPageViewController];

    [self.view addSubview:mPageViewController.view];

    [self.view sendSubviewToBack:mPageViewController.view];



2. then implement it's delegate methods

        - (UIViewController *)pageViewController:(UIPageViewController *)pvc viewControllerBeforeViewController:(YourViewController *)viewController
        { // take on mPageIndex property in your controller to detect current index

            if (viewController.mPageIndex > 0) {

            YourViewController * theVC = [[YourViewController alloc] init];
            theVC.mPageIndex=viewController.mPageIndex-1;
            return theVC;
        }

        return nil;
        }

        - (UIViewController *)pageViewController:(UIPageViewController *)pvc viewControllerAfterViewController:(PhotoViewController *)viewController
       {
            if (viewController.mPageIndex < mControllerListArray.count-1) {

        YourViewController * theVC = [[YourViewController alloc] init];
        theVC.mPageIndex=viewController.mPageIndex+1; // mPageIndex is propety of type int to detect current page
        return theVC;
    }

        return nil;
}


-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
        {
        if (completed) {

            NSUInteger currentIndex = [[mPageViewController.viewControllers lastObject] mPageIndex];

            mPageIndex = (int)currentIndex;

            [self updateUI];

       }
   }