How do I make the bottom bar with dots of a UIPageViewController translucent?

前端 未结 12 1865
逝去的感伤
逝去的感伤 2020-12-04 07:37

I\'m in the process of making a tutorial, and I\'m trying to emulate the style of Path\'s tutorial like so:

http://www.appcoda.com/wp-content/uploads/2013/06/UIPageV

相关标签:
12条回答
  • 2020-12-04 08:02

    this code is in Swift Add following in your 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()
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 08:03

    I solve using this code:

    - (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];
    
    }
    
    0 讨论(0)
  • 2020-12-04 08:06

    I found an other workarround that fits me better.

    I reuse the code given by zerotool to get the UIPageControl (var called pageControl) and the UIScrollView (var called pageView) used by the UIPageViewController.

    Once that done in the viewDidLoad, I just prevent clip subview of pageView and let the content spread more to be beneath the UIPageControl.

    The pageControl is beneath the pageView so we have to manually make it come in front.

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

    Once I get my pageView covering the space occupied by the pageControl but under the pageControl, I just have to adjust the nib file use for each viewController displayed as page :

    • base view should not clip
    • the first and only subview :
      • should have constraint to set bottom to -37 (or more if you need but 37 is the size of the pageControl) from bottom of superview
      • should clip content
    0 讨论(0)
  • 2020-12-04 08:12

    It is very easy to make it work. You just only have to make the pageviewcontroller taller, and place a PageControl into the XIB file. The trick is put the PageControl in the foreground (and all the other common controls) at the beginning, and update the content of the PageControl with the PageViewController. Here is the code:

    - (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;
    }
    
    0 讨论(0)
  • 2020-12-04 08:17

    The same effect can be achieved simply by subclassing UIPageViewController and overriding viewDidLayoutSubviews as follows:

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

    Then there is no need to maintain a separate UIPageControl and such.

    0 讨论(0)
  • 2020-12-04 08:17

    Swift 3 snippet

    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)
        }
    }
    
    0 讨论(0)
提交回复
热议问题