UIPageViewController和UIButton混合使用 [英] UIPageViewController and UIButton mix use

查看:133
本文介绍了UIPageViewController和UIButton混合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here's the thing: my app needs a series of tutorial screens at the beginning when people launch it, along with two buttons on the bottom of the screen to let people register or login. (like the image shows)

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

My question is : how can I keep the buttons on the screen when people slide the tutorial images ?

I尝试了几种方法:


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

  1. In PageContentViewController (subclass of UIViewController), I have a UIImageView for the tutorial image, and two UIButton for the buttons. This way, the buttons also get slide, not what I want.

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

I use a new ViewController for the buttons, and in my rootviewController, I add this new viewcontroller as a subview. This way, the new viewcontroller hides the tutorial images underneath it. I can't slide the image. I also tried to set the alpha value of the new viewcontroller, so that I can see the images underneath it, but still can't slide it.

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

Any better ideas or sample code I can learn from ?

推荐答案

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

This is very simple. The trick is to add the buttons "on top of " your page view controller view. That is, after you add the page view controller, you can use the sendSubviewToBack method to ensure that the buttons remain in the foreground while you will be able to swipe between the different view controllers of your page view controller. You can understand this behaviour by simply following these steps:


  1. 打开Xcode。

  2. 创建新的项目并选择基于页面的应用程序。

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

Xcode will create a basic app with a UIPageViewController implementation in the RootViewController. In RootViewController s viewDidLoad method, you will see the following code:

- (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添加到其视图中。

Now Take the Main.storyboard and you will see the RootViewController Scene.Add a UIButton to its view using the interface builder.

现在添加以下内容viewDidLoad方法结束时的行。

Now add the following line at the end of the viewDidLoad method.

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

并试一试!你会看到它正是你想要的。

And give it a run! you will see that it is just what you wanted.

查看输出截图:


< img src =https://i.stack.imgur.com/nUYdV.pngalt =Third>

See the screenshots of the output:

这篇关于UIPageViewController和UIButton混合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆