[英]Setting up UIScrollView to swipe between 3 view controllers
I am trying to set up a UIScrollView so that I can swipe between my 3 view controllers. 我正在尝试设置UIScrollView,以便我可以在我的3个视图控制器之间滑动。 This is my code in AppDelegate.m:
这是我在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. 基本上,我只想让整个屏幕成为一个滚动视图,它允许我在我的3个视图控制器之间滑动。 How would I go about doing that?
我该怎么做呢?
UPD: June, 2015 Swift UPD:2015年6月 Swift
The concept remains the same, which is described below in Objective-C section. 概念保持不变,这将在下面的Objective-C部分中描述。 There is a little change in syntax.
语法有一点变化。 To add childviewcontroller use following snippet:
要添加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 Objective-C的
Create your own custom container view controller (I will call it combinedViewController), which will hold your three controllers in scroll view. 创建自己的自定义容器视图控制器(我将其称为combinedViewController),它将在滚动视图中保存您的三个控制器。 Inherit like you always do UIViewController, then use addChildViewController public API in your new combinedViewController
-viewDidLoad:
like this: 像你总是做UIViewController一样继承,然后在你的新的combinedViewController
-viewDidLoad:
使用addChildViewController公共API -viewDidLoad:
像这样:
[self addChildViewController:aViewController];
[self.scrollView addSubview:aViewController.view];
[aViewController didMoveToParentViewController:self];
Here's what the code does: 这是代码的作用:
Do this operation with each of your viewControllers. 对每个viewControllers执行此操作。 Afterwards, set your combinedViewController as a rootViewController.
然后,将combinedViewController设置为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 在这里,您是我的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: 结果:
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)
Swift 3.0 Swift 3.0
Based on Sachin answer - bit more generic - just add next element to views array 基于Sachin的答案 - 更通用 - 只需将下一个元素添加到views数组中
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($0) })
_ = views.map({ $0.view.frame.origin = CGPoint(x: CGFloat(views.index(of: $0)!) * width, y: 0) })
}
func addViewToScrollView(_ viewController: UIViewController) {
scrollView.addSubview(viewController.view)
views.willMove(toParentViewController: self)
addChildViewController(viewController)
}
Try this git repo, Using this repo you can create a view navigation like Snapchat/Tinder Main Pages. 试试这个git repo,使用这个repo你可以创建像Snapchat / Tinder主页这样的视图导航。
https://github.com/goktugyil/EZSwipeController https://github.com/goktugyil/EZSwipeController
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.