gpt4 book ai didi

ios - UIPageViewController 和 UIButton 混合使用

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:26:51 27 4
gpt4 key购买 nike

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

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

我尝试了几种方法:

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

  2. 我为按钮使用了一个新的 ViewController,在我的 rootviewController 中,我将这个新的 viewcontroller 添加为 subview 。这样,新的 View Controller 将教程图像隐藏在它下面。我无法滑动图像。我也试过设置新viewcontroller的alpha值,这样可以看到下面的图片,但是还是不能滑动。

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

最佳答案

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

  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;


}

现在使用 Main.storyboard,您将看到 RootViewController 场景。使用界面构建器将 UIButton 添加到其 View 。 screenshot

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

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

然后试一试!您会发现这正是您想要的。

查看输出截图: First Second Third

关于ios - UIPageViewController 和 UIButton 混合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27581524/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com