设置 UIScrollView 在 3 个视图控制器之间滑动

Setting up UIScrollView to swipe between 3 view controllers(设置 UIScrollView 在 3 个视图控制器之间滑动)
本文介绍了设置 UIScrollView 在 3 个视图控制器之间滑动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试设置 UIScrollView,以便可以在 3 个视图控制器之间滑动.这是我在 AppDelegate.m 中的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];//应用程序启动后自定义的覆盖点.;UIScrollView *sv = [[UIScrollView alloc] init];BarsViewController *bvc = [[BarsViewController alloc] init];//创建 BarsViewControllerStopwatchViewController *svc = [[StopwatchViewController alloc] init];//创建 StopwatchViewControllerTimerViewController *tvc = [[TimerViewController alloc] init];//创建 TimerViewController[sv addSubview:bvc.view];[sv addSubview:svc.view];[sv addSubview:tvc.view];[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];//隐藏状态栏self.window.rootViewController = sv;[self.window makeKeyAndVisible];返回是;}

它在这一行给出一个错误:

self.window.rootViewController = sv;

说,不兼容的指针类型从 UIScrollView * 分配给 'UIViewController *'".

但是,没有 UIScrollViewController 这样的东西,所以我不知道该怎么做.

基本上,我只想让整个屏幕成为一个滚动视图,让我可以在我的 3 个视图控制器之间滑动.我该怎么做呢?

解决方案

UPD:2015 年 6 月斯威夫特

这个概念保持不变,这在下面的 Objective-C 部分中进行了描述.语法有一点变化.要添加 childviewcontroller,请使用以下代码段:

让 aViewController = storyboard.instantiateViewControllerWithIdentifier("A") as!视图控制器;addChildViewController(aViewController);scrollView!.addSubview(aViewController.view)aViewController.didMoveToParentViewController(self)

查看我的

I am trying to set up a UIScrollView so that I can swipe between my 3 view controllers. This is my code in AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.;

UIScrollView *sv = [[UIScrollView alloc] init];

BarsViewController *bvc = [[BarsViewController alloc] init]; // Create BarsViewController
StopwatchViewController *svc = [[StopwatchViewController alloc] init]; // Create StopwatchViewController
TimerViewController *tvc = [[TimerViewController alloc] init]; // Create TimerViewController

[sv addSubview:bvc.view];
[sv addSubview:svc.view];
[sv addSubview:tvc.view];

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade]; // Hide status bar

self.window.rootViewController = sv;
[self.window makeKeyAndVisible];
return YES;
}

It gives an error on this line:

self.window.rootViewController = sv;

saying, "Incompatible pointer types assigning to 'UIViewController *' from UIScrollView *'".

However, there is no such thing as a UIScrollViewController, so I don't know what to do.

Basically, I just want the whole screen to be a scroll view which allows me to swipe between my 3 view controllers. How would I go about doing that?

解决方案

UPD: June, 2015 Swift

The concept remains the same, which is described below in Objective-C section. There is a little change in syntax. To add childviewcontroller use following snippet:

let aViewController = storyboard.instantiateViewControllerWithIdentifier("A") as! AViewController;

addChildViewController(aViewController);
scrollView!.addSubview(aViewController.view)
aViewController.didMoveToParentViewController(self)

Check my Swift Github Sample Code

Objective-C

Create your own custom container view controller (I will call it combinedViewController), which will hold your three controllers in scroll view. Inherit like you always do UIViewController, then use addChildViewController public API in your new combinedViewController -viewDidLoad: like this:

[self addChildViewController:aViewController];
[self.scrollView addSubview:aViewController.view];
[aViewController didMoveToParentViewController:self];

Here’s what the code does:

  • It calls the container’s addChildViewController: method to add the child.
  • It accesses the child’s view property to retrieve the view and adds it to its own view hierarchy. The container sets the child’s size and position before adding the view; containers always choose where the child’s content appears.
  • It explicitly calls the child’s didMoveToParentViewController: method to signal that the operation is complete.

Do this operation with each of your viewControllers. Afterwards, set your combinedViewController as a rootViewController.

if you need further explanation, feel free to ask.

Reference: Design custom container view controller

Here you are my Objective-C Github sample code

UPD: Thanks @Oliver Atkinson for clarifying that addChildViewController: method also calls the child’s willMoveToParentViewController: method automatically.

Results:

这篇关于设置 UIScrollView 在 3 个视图控制器之间滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

UIButtons at the bottom of UIScrollView not working(UIScrollView 底部的 UIButtons 不起作用)
scrollViewWillEndDragging:withVelocity:targetContentOffset: not working on the edges of a UISCrollView(scrollViewWillEndDragging:withVelocity:targetContentOffset: 不在 UISCrollView 的边缘工作)
ImageView Scaling when scrolling down(向下滚动时 ImageView 缩放)
Bounds automatically changes on UIScrollView with content insets(UIScrollView 上的边界自动更改,带有内容插图)
iOS5 UITapRecognizer for UIScrollView interfering with buttons. How to fix?(用于 UIScrollView 的 iOS5 UITapRecognizer 干扰按钮.怎么修?)
Handling scroll views with (custom, interactive) view controller presentation and dismissal(使用(自定义、交互式)视图控制器呈现和解除处理滚动视图)