UIPageViewController和UIButton混合使用

col*_*lin 2 iphone uibutton ios uipageviewcontroller

事情就是这样:我的应用程序在人们启动它时需要一系列教程屏幕,以及屏幕底部的两个按钮,以便人们注册或登录.(如图所示) 例

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

我试过几种方法:

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

  2. 我为按钮使用了一个新的ViewController,在我的rootviewController中,我将这个新的viewcontroller添加为子视图.这样,新的viewcontroller会隐藏它下面的教程图像.我无法滑动图像.我还试图设置新的viewcontroller的alpha值,这样我就可以看到它下面的图像,但仍然无法滑动它.

我可以从中学到更好的想法或示例代码吗?

小智 5

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

  1. 打开Xcode.
  2. 创建一个新项目并选择"基于页面的应用程序".

Xcode将在RootViewController中创建一个带有UIPageViewController实现的基本应用程序.在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;


}
Run Code Online (Sandbox Code Playgroud)

现在使用Main.storyboard,您将看到RootViewController Scene.使用界面构建器将UIButton添加到其视图中. 截图

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

[self.view sendSubviewToBack:self.pageViewController.view];
Run Code Online (Sandbox Code Playgroud)

并试一试!你会发现它正是你想要的.

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