45

我正在制作教程,我正在尝试模仿 Path 教程的风格,如下所示:

http://www.appcoda.com/wp-content/uploads/2013/06/UIPageViewController-Tutorial-Screen.jpg 在此处输入图像描述

我的问题是,如果将委托方法设置为:

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
    // The number of items reflected in the page indicator.
    return 5;
}

然后我在点下得到这个愚蠢的黑条:

http://i.stack.imgur.com/pUEdh.png 在此处输入图像描述

有没有办法以类似于将 UINavigationBar 设置为半透明的方式使该栏半透明?

4

12 回答 12

47

让它工作很容易。您只需要使 pageviewcontroller 更高,并将 PageControl 放入 XIB 文件中。诀窍是一开始就把 PageControl 放在前台(以及所有其他常用控件),然后用 PageViewController 更新 PageControl 的内容。这是代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

    self.pageController.dataSource = self;
    // We need to cover all the control by making the frame taller (+ 37)
    [[self.pageController view] setFrame:CGRectMake(0, 0, [[self view] bounds].size.width, [[self view] bounds].size.height + 37)];

    TutorialPageViewController *initialViewController = [self viewControllerAtIndex:0];

    NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];

    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    [self addChildViewController:self.pageController];
    [[self view] addSubview:[self.pageController view]];
    [self.pageController didMoveToParentViewController:self];

    // Bring the common controls to the foreground (they were hidden since the frame is taller)
    [self.view bringSubviewToFront:self.pcDots];
    [self.view bringSubviewToFront:self.btnSkip];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {

    NSUInteger index = [(TutorialPageViewController *)viewController index];

    [self.pcDots setCurrentPage:index];

    if (index == 0) {
        return nil;
    }

    index--;

    return [self viewControllerAtIndex:index];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {

    NSUInteger index = [(TutorialPageViewController *)viewController index];

    [self.pcDots setCurrentPage:index];

    index++;

    if (index == 3) {
        return nil;
    }

    return [self viewControllerAtIndex:index];
}

- (TutorialPageViewController *)viewControllerAtIndex:(NSUInteger)index {

    TutorialPageViewController *childViewController = [[TutorialPageViewController alloc] initWithNibName:@"TutorialPageViewController" bundle:nil];
    childViewController.index = index;

    return childViewController;
}

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
    // The number of items reflected in the page indicator.
    NSInteger tutorialSteps = 3;
    [self.pcDots setNumberOfPages:tutorialSteps];

    return tutorialSteps;
}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
    // The selected item reflected in the page indicator.
    return 0;
}
于 2013-10-02T15:14:40.143 回答
21

只需继承 UIPageViewController 并覆盖 viewDidLayoutSubviews 即可实现相同的效果,如下所示:

-(void)viewDidLayoutSubviews {
    UIView* v = self.view;
    NSArray* subviews = v.subviews;
    // Confirm that the view has the exact expected structure.
    // If you add any custom subviews, you will want to remove this check.
    if( [subviews count] == 2 ) {
        UIScrollView* sv = nil;
        UIPageControl* pc = nil;
        for( UIView* t in subviews ) {
            if( [t isKindOfClass:[UIScrollView class]] ) {
                sv = (UIScrollView*)t;
            } else if( [t isKindOfClass:[UIPageControl class]] ) {
                pc = (UIPageControl*)t;
            }
        }
        if( sv != nil && pc != nil ) {
            // expand scroll view to fit entire view
            sv.frame = v.bounds;
            // put page control in front
            [v bringSubviewToFront:pc];
        }
    }
    [super viewDidLayoutSubviews];
}

那么就没有必要维护一个单独的 UIPageControl 之类的了。

于 2014-07-20T15:26:46.947 回答
9

斯威夫特 3 片段

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    if let scrollView = view.subviews.filter({ $0 is UIScrollView }).first,
        let pageControl = view.subviews.filter({ $0 is UIPageControl }).first {
        scrollView.frame = view.bounds
        view.bringSubview(toFront:pageControl)
    }
}
于 2016-10-24T08:26:23.430 回答
6

这是 Zerotool 的解决方案到 Swift 2.1 的转换,尽管可能有一种更优雅的方式来编写它:

override func viewDidLayoutSubviews() {
    var scrollView: UIScrollView?
    var pageControl: UIPageControl?

    // If you add any custom subviews, you will want to remove this check.
    if (self.view.subviews.count == 2) {
        for view in self.view.subviews {
            if (view.isKindOfClass(UIScrollView)) {
                scrollView = view as? UIScrollView
            } else if (view.isKindOfClass(UIPageControl)) {
                pageControl = view as? UIPageControl
            }
        }
    }

    if let scrollView = scrollView {
        if let pageControl = pageControl {
            scrollView.frame = self.view.bounds
            self.view.bringSubviewToFront(pageControl)
        }
    }

    super.viewDidLayoutSubviews()
}
于 2016-02-28T06:30:01.097 回答
4

我认为您无法更改 UIPageViewController 的行为,因此 Path 应用程序似乎很可能使用自己的视图控制器。您也可以这样做:创建您自己的容器视图控制器,该控制器使用 UIPageControl 来指示当前页面。

于 2013-07-22T04:47:46.920 回答
3

您可以简单地调整 UIPageViewController 的 UIPageControl 的 alpha。

首先,您应该像这样从 UIPageViewController 中检索它:

- (UIPageControl *)getPageControlForPageViewController:(UIPageViewController *)pageViewController {
      for (UIView *subview in self.pageViewController.view.subviews) {
        if ([subview isKindOfClass:[UIPageControl class]]) {
          return (UIPageControl *) subview;
        }
      }

      return nil;
    }

接下来,使用该功能。我在 ViewController 上创建了一个名为 childPageControl 的属性。给它 UIPageViewController 的 UIPageControl:

self.childPageControl = [self getPageControlForPageViewController:self.pageViewController];

接下来,您可以调整 alpha 以提供半透明效果:

self.childPageControl.alpha = .5;

您在影响 UIPageViewController 的 UIPageControl 方面可以做的事情非常有限,但您至少可以轻松实现这一点。

于 2014-05-30T19:39:11.753 回答
2

此代码在 Swift 在您的 UIPageViewController 中添加以下内容

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

    for view in self.view.subviews {
        if view.isKindOfClass(UIScrollView) {
            view.frame = UIScreen.mainScreen().bounds
        } else if view.isKindOfClass(UIPageControl) {
            view.backgroundColor = UIColor.clearColor()
        }
    }
}
于 2016-05-09T11:40:39.203 回答
2

我今天发现的小技巧..

请看下面的代码。

在此处输入图像描述

self.pageController.dataSource = self;
    CGRect rect = [self.view bounds];
    rect.size.height+=37;
    [[self.pageController view] setFrame:rect];
        NSArray *subviews = self.pageController.view.subviews;
        UIPageControl *thisControl = nil;
        for (int i=0; i<[subviews count]; i++) {
            if ([[subviews objectAtIndex:i] isKindOfClass:[UIPageControl class]]) {
                thisControl = (UIPageControl *)[subviews objectAtIndex:i];
            }
        }

        UIView *tempview = [[UIView alloc] initWithFrame:CGRectMake(0, -30, 320, 40)];
        [tempview addSubview:thisControl];
        thisControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        thisControl.currentPageIndicatorTintColor = [UIColor greenColor];
        [self.view addSubview:tempview];
于 2016-02-11T19:51:22.800 回答
1

我使用以下代码解决:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.


self.namesImage = @[@"page1.png", @"page2.png", @"page3.png", @"page4.png"];

self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
self.pageViewController.dataSource = self;


TutorialContentViewController *startingViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = @[startingViewController];

[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];


[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];

[[UIPageControl appearance] setPageIndicatorTintColor:[UIColor grayColor]];
[[UIPageControl appearance] setCurrentPageIndicatorTintColor:[UIColor whiteColor]];
[[UIPageControl appearance] setBackgroundColor: [[UIColor blackColor] colorWithAlphaComponent:0.1f]];
[[UIPageControl appearance] setOpaque:YES];

}
于 2016-11-25T19:53:10.180 回答
1

斯威夫特 5.2

您可以将此代码用于您的要求

 override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    if let myScrollView = view.subviews.filter({ $0 is UIScrollView }).first,
        let myPageControl = view.subviews.filter({ $0 is UIPageControl }).first {
        myScrollView.frame = view.bounds
        view.bringSubviewToFront(myPageControl)
    }
}
于 2020-01-29T12:41:56.697 回答
1

我想在我正在开发的应用程序中做类似的效果——我使用了一个 UIPageViewController 和一个单独的 UIPageControl。

这使您可以将 UIPageControl 放置在视图中您想要的任何位置,包括 UIPageViewController 的顶部,并通过 UIPageViewController 委托方法保持其活动页面点为最新:

- (void)pageViewController:(UIPageViewController *)pageViewController
        didFinishAnimating:(BOOL)finished
   previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers
       transitionCompleted:(BOOL)completed {
    if (completed) {
        self.pageControl.currentPage = [self.pageViewControllers indexOfObject:pageViewController.viewControllers.firstObject];
    }
}

无需遍历子视图层次结构试图找到内部的 UIPageViewController 页面控件,也不必调整内部滚动视图的内容大小。

希望这可以帮助。

于 2015-10-30T21:01:21.283 回答
0

我找到了另一个更适合我的解决方法。

我重用zerotool 给出的代码来获取 UIPageViewController 使用的 UIPageControl(称为 pageControl 的变量)和 UIScrollView(称为 pageView 的变量)。

一旦在 viewDidLoad 中完成,我将阻止 pageView 的剪辑子视图,并让内容更多地传播到 UIPageControl 下方。

pageControl 位于 pageView 下方,因此我们必须手动将其置于前面。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    if(
       [[[self view] subviews] count] == 2
       )
    {
        UIScrollView* pageView = nil;
        UIPageControl* pageControl = nil;

        UIView* selfView = self.view;
        NSArray* subviews = selfView.subviews;

        for( NSInteger i = 0 ; i < subviews.count && ( pageView == nil || pageControl == nil ) ; i++ )
        {
            UIView* t = subviews[i];
            if( [t isKindOfClass:[UIScrollView class]] )
            {
                pageView = (UIScrollView*)t;
            }
            else if( [t isKindOfClass:[UIPageControl class]] )
            {
                pageControl = (UIPageControl*)t;
            }
        }

        if( pageView != nil && pageControl != nil )
        {
            [pageView setClipsToBounds:NO];
            [selfView bringSubviewToFront:pageControl];
        }
    }
}

一旦我的 pageView 覆盖了 pageControl 但在 pageControl 下占用的空间,我只需要调整显示为 page 的每个 viewController 的 nib 文件使用:

  • 基础视图不应剪辑
  • 第一个也是唯一的子视图:
    • 应该有约束从超级视图的底部将底部设置为-37(如果需要,可以设置更多,但 37 是 pageControl 的大小)
    • 应该剪辑内容
于 2015-03-04T11:17:04.513 回答