OGeek|极客世界-中国程序员成长平台

标题: ios - UIPageViewController 与多个 UIViewController 与标题和底栏 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 09:49
标题: ios - UIPageViewController 与多个 UIViewController 与标题和底栏

我用多个 UIViewController 做了一个 UIPageViewController。现在,我需要在 UIPageViewController 的顶部和底部添加一个固定的 UIView...

这是我的代码:

   - (void)viewDidLoad
{
    [super viewDidLoad];

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

    // Point the datasource back to this UIViewController.
    self.pageController.dataSource = self;

    // Assuming you have a SomePageViewController which extends UIViewController so you can do custom things.


    UIStoryboard *sb = [UIStoryboard storyboardWithName"Main" bundle:nil];
    if(!self.roteirosView)
        self.roteirosView = [sb instantiateViewControllerWithIdentifier"RoteirosView"];

    self.initialViewControllers = [NSArray arrayWithObject:self.roteirosView];

    // animated:NO is important so the view just pops into existence.
    // direction: doesn't matter because it's not animating in.
    [self.pageController setViewControllers:self.initialViewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    // Tell the child view to get ready
    [self.pageController willMoveToParentViewController:self];

    // Actually add the child view controller
    [self addChildViewController:self.pageController];

    // Don't forget to add the new root view to the current view hierarchy!
    [self.view addSubview:self.pageController.view];

    // And make sure to activate!
    [self.pageController didMoveToParentViewController:self];
}

- (void)viewDidLayoutSubviews {
}

- (UIViewController *)viewControllerAtIndexint)i {
    // Asking for a page that is out of bounds??
    if (i<0) {
        return nil;
    }
    if (i>=MAX_PAGES) {
        return nil;
    }

    // Assuming you have SomePageViewController.xib
    UIStoryboard *sb = [UIStoryboard storyboardWithName"Main" bundle:nil];

    if (i == 0) {
        if(!self.roteirosView)
            self.roteirosView = [sb instantiateViewControllerWithIdentifier"RoteirosView"];
        return self.roteirosView;
    }
    else {
        if(!self.pertoView)
            self.pertoView = [sb instantiateViewControllerWithIdentifier"ertoView"];
        return self.pertoView;
    }
}


- (UIViewController *)pageViewControllerUIPageViewController *)pageViewController viewControllerBeforeViewControllerUIViewController *)viewController {
    UIViewController *p = (UIViewController *)viewController;
    if ([p isKindOfClass:[PertoViewController class]]) {
        return [self viewControllerAtIndex:0];
    }
    else {
        return [self viewControllerAtIndex:MAX_PAGES]; // permanece na mesma página
    }

}


- (UIViewController *)pageViewControllerUIPageViewController *)pageViewController viewControllerAfterViewControllerUIViewController *)viewController {
    UIViewController *p = (UIViewController *)viewController;
    if ([p isKindOfClass:[PertoViewController class]]) {
        return [self viewControllerAtIndex:MAX_PAGES]; // permanece na mesma página
    }
    else {
        return [self viewControllerAtIndex:1];
    }
}

在我的 UIStoryBoard 中有 HomesViewController,它是我的 UIPageViewController、PertoViewController 和 RoteirosViewController。 UIViewControllers 都添加在 UIPageViewController 中。



Best Answer-推荐答案


在 UIPagerViewController 类的 viewDidAppear 中添加您要添加​​的 2 个 UIView,这应该在 2 个 View Controller 中添加新 View 而不是已加载的 View 。

如果这对您不起作用,请确保您也通过代码而不是 Storyboard 中的代码添加 2 个 View Controller ,并确保先添加它们,然后添加 2 个 View 标题和底部一个以确保它们在屏幕上

希望这个答案可以帮助你,祝你好运

关于ios - UIPageViewController 与多个 UIViewController 与标题和底栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31965739/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4