我正在尝试设置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;
}
它在这一行上出错:
self.window.rootViewController = sv;
说,“不兼容的指针类型从UIScrollView *'分配给'UIViewController *'。”
但是,没有UIScrollViewController这样的东西,所以我不知道该怎么做。
基本上,我只想让整个屏幕成为一个滚动视图,它允许我在我的3个视图控制器之间滑动。我该怎么做呢?
答案 0 :(得分:39)
UPD:2015年6月 的夫特强>
概念保持不变,这将在下面的Objective-C部分中描述。语法有一点变化。要添加childviewcontroller,请使用以下代码段:
let aViewController = storyboard.instantiateViewControllerWithIdentifier("A") as! AViewController;
addChildViewController(aViewController);
scrollView!.addSubview(aViewController.view)
aViewController.didMoveToParentViewController(self)
<强>目标C 强>
创建您自己的自定义容器视图控制器(我将其称为combinedViewController),它将在滚动视图中保存您的三个控制器。
像你总是做UIViewController一样继承,然后在你的新的combinedViewController -viewDidLoad:
中使用addChildViewController公共API,如下所示:
[self addChildViewController:aViewController];
[self.scrollView addSubview:aViewController.view];
[aViewController didMoveToParentViewController:self];
以下是代码的作用:
对每个viewControllers执行此操作。然后,将combinedViewController设置为rootViewController。
如果您需要进一步解释,请随时提出。
参考:Design custom container view controller
此处您是我的Objective-C Github sample code
UPD:感谢@Oliver Atkinson澄清addChildViewController:
方法也会自动调用孩子的willMoveToParentViewController:
方法。
<强>结果:强>
答案 1 :(得分:2)
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)
答案 2 :(得分:2)
Swift 3.0
基于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($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)
}
答案 3 :(得分:1)
试试这个git repo,使用这个repo你可以创建一个像Snapchat / Tinder主页这样的视图导航。