UIPageViewController和UIButton混合使用

事情就是这样的:我们的应用程序在开始时需要一系列的教程屏幕,以及屏幕底部的两个button,以便人们注册或login。 (如图所示) 例

我的问题是:当人们滑动教程图像时,如何将button保留在屏幕上?

我尝试了几种方法:

  1. 在PageContentViewController(UIViewController的子类)中,我有一个用于教程图像的UIImageView,以及两个用于button的UIButton。 这样,button也滑动,而不是我想要的。

  2. 我使用一个新的ViewController的button,并在我的rootviewController,我添加这个新的viewcontroller作为子视图。 这样,新的viewcontroller就隐藏了它下面的教程图片。 我无法滑动图像。 我也尝试设置新的viewcontroller的alpha值,以便我可以看到它下面的图像,但仍然不能滑动它。

任何更好的想法或示例代码,我可以学习?

这很简单。 诀窍是在页面视图控制器视图的顶部添加button。 也就是说,在添加页面视图控制器之后,可以使用sendSubviewToBack方法来确保button保持在前景中,同时您可以在页面视图控制器的不同视图控制器之间进行滑动。 您只需按照以下步骤即可了解这一行为:

  1. 打开Xcode。
  2. 创build一个新项目并select“基于页面的应用程序”。

Xcode将在RootViewController中使用UIPageViewController实现创build一个基本的应用程序。 在RootViewController的viewDidLoad方法中,您将看到以下代码:

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // Configure the page view controller and add it as a child view controller. self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; self.pageViewController.delegate = self; DataViewController *startingViewController = [self.modelController viewControllerAtIndex:0 storyboard:self.storyboard]; NSArray *viewControllers = @[startingViewController]; [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; self.pageViewController.dataSource = self.modelController; [self addChildViewController:self.pageViewController]; [self.view addSubview:self.pageViewController.view]; // Set the page view controller's bounds using an inset rect so that self's view is visible around the edges of the pages. CGRect pageViewRect = self.view.bounds; self.pageViewController.view.frame = pageViewRect; [self.pageViewController didMoveToParentViewController:self]; // Add the page view controller's gesture recognizers to the book view controller's view so that the gestures are started more easily. self.view.gestureRecognizers = self.pageViewController.gestureRecognizers; } 

现在转到Main.storyboard,你会看到RootViewController Scene。使用界面生成器在其视图中添加一个UIButton。 截图

现在,在viewDidLoad方法的末尾添加以下行。

 [self.view sendSubviewToBack:self.pageViewController.view]; 

给它一个跑步! 你会看到,这正是你想要的。

查看输出的屏幕截图: 第一第二第三