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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19820939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:47:38  来源:igfitidea点击:

Setting up UIScrollView to swipe between 3 view controllers

iosobjective-cswiftxcodeuiscrollview

提问by user2397282

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:

我正在尝试设置一个 UIScrollView 以便我可以在我的 3 个视图控制器之间滑动。这是我在 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 *'".

说,“从 UIScrollView *' 分配给'UIViewController *' 的不兼容的指针类型”。

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

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

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?

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

回答by Neil Galiaskarov

UPD: June, 2015Swift

UPD:六月,2015年斯威夫特

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:

概念保持不变,这将在下面的 Objective-C 部分进行描述。语法略有变化。要添加 childviewcontroller 使用以下代码段:

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

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

Check my Swift Github Sample Code

检查我的Swift Github 示例代码

Objective-C

目标-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:

创建您自己的自定义容器视图控制器(我将其称为组合视图控制器),它将在滚动视图中保存您的三个控制器。像往常一样继承 UIViewController,然后在新的组合视图控制器中使用 addChildViewController 公共 API,-viewDidLoad:如下所示:

[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.
  • 它调用容器的 addChildViewController: 方法来添加孩子。
  • 它访问子视图属性以检索视图并将其添加到自己的视图层次结构中。容器在添加视图之前设置孩子的大小和位置;容器总是选择子内容出现的位置。
  • 它显式调用子节点的 didMoveToParentViewController: 方法来发出操作完成的信号。

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

对每个 viewController 执行此操作。之后,将您的组合视图控制器设置为根视图控制器。

if you need further explanation, feel free to ask.

如果您需要进一步解释,请随时询问。

Reference: Design custom container view controller

参考:设计自定义容器视图控制器

Here you are my Objective-C Github sample code

这是我的Objective-C Github 示例代码

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

UPD:感谢@Oliver Atkinson 澄清该addChildViewController:方法也会willMoveToParentViewController:自动调用孩子的方法。

Results:

结果:

enter image description here

在此处输入图片说明

回答by sachin

    let obj1 = self.storyboard?.instantiateViewControllerWithIdentifier("DocumentsVC") as! DocumentsVC
    let obj2 = self.storyboard?.instantiateViewControllerWithIdentifier("AppointmentsVC") as! AppointmentsVC
    let obj3 = self.storyboard?.instantiateViewControllerWithIdentifier("DashboardVC") as! DashboardVC

    self.containerScrollView.frame = obj2.view.frame

    self.containerScrollView.addSubview(obj2.view)
    obj2.willMoveToParentViewController(self)
    self.addChildViewController(obj2)

    self.containerScrollView.addSubview(obj1.view)
    obj1.willMoveToParentViewController(self)
    self.addChildViewController(obj1)

    self.containerScrollView.addSubview(obj3.view)
    obj3.willMoveToParentViewController(self)
    self.addChildViewController(obj3)

    self.containerScrollView.contentSize = CGSizeMake(3*UIScreen.mainScreen().bounds.width, 0)

    obj1.view.frame.origin =  CGPointMake(0, 0)
    obj2.view.frame.origin =  CGPointMake(UIScreen.mainScreen().bounds.width, 0)
    obj3.view.frame.origin = CGPointMake(2*UIScreen.mainScreen().bounds.width, 0)

回答by chrisco

Swift 3.0

斯威夫特 3.0

Based on Sachin answer - bit more generic - just add next element to views array

基于 Sachin 的回答 - 更通用一点 - 只需将下一个元素添加到视图数组

var views = [ViewController(), ViewController2(), ViewController3(), ViewController4()]

func setupScrollView() {
    scrollView.frame = views.first!.view.frame
    scrollView.contentSize = CGSize(width: CGFloat(views.count) * width, height: 0)
    _ = views.map({ addViewToScrollView(##代码##) })
    _ = views.map({ ##代码##.view.frame.origin =  CGPoint(x: CGFloat(views.index(of: ##代码##)!) * width, y: 0) })
}

func addViewToScrollView(_ viewController: UIViewController) {
    scrollView.addSubview(viewController.view)
    views.willMove(toParentViewController: self)
    addChildViewController(viewController)
}

回答by Jayesh Thanki

Try this git repo, Using this repo you can create a view navigation like Snapchat/Tinder Main Pages.

试试这个 git repo,使用这个 repo,你可以创建一个视图导航,比如 Snapchat/Tinder Main Pages。

https://github.com/goktugyil/EZSwipeController

https://github.com/goktugyil/EZSwipeController