呈现透明背景的模态视图控制器
我想以编程的方式呈现一个UIViewController
,它应该以透明的背景显示(或不)。 我想要iOS 7.0及以上版本。 我发现自己有很多问题(和答案),但他们无法帮助我。 这是我的应用程序的视图层次结构。
我正在使用侧边菜单控制器(RESideMenu)。
我有一个rootView(来自RESideMenu的基地) – >显示UINavigationController
的中心控制器(以及一个左视图控制器)。
在要求中,我想介绍一个视图控制器
从推送的视图控制器(在导航层次结构中)
从呈现的视图控制器(在导航层级中)
另外,我需要介绍它并执行一些操作,然后将其删除。
我很确定,这应该在很多情况下,有(或没有)侧边菜单,甚至导航控制器。
我将一个单独的问题(当然也包括答案)发布到这个队列中,因为我认为这对于社区开发者来说可能是有帮助的,他们也可能因为缺乏可接受的解决scheme而感到沮丧。
假设我们在
FirstViewController
//Obj-C - (void) presentSecondVC { SecondViewController *vc = [[SecondViewController alloc] init]; [self addChildViewController:vc]; [self didMoveToParentViewController:vc]; } //Swift func presentSecondVC() { let vc = SecondViewController.init() self.addChildViewController(vc) self.didMove(toParentViewController: vc) }
有些人可能需要像这样写上面的方法,
//Obj-C - (void) presentSecondVC { SecondViewController *vc = [[SecondViewController alloc] init]; vc.view.frame = CGRectMake(0,0,width,height); //Your own CGRect [self.view addSubview:vc.view]; //If you don't want to show inside a specific view [self addChildViewController:vc]; [self didMoveToParentViewController:vc]; //for someone, may need to do this. //[self.navigationController addChildViewController:vc]; //[self.navigationController didMoveToParentViewController:vc]; } //Swift func presentSecondVC() { let vc = SecondViewController.init() vc.view.frame = CGRect.init(x: 0, y: 0, width: width, height: height) //Your own CGRect self.view.addSubview(vc.view) //If you don't want to show inside a specific view. self.addChildViewController(vc) self.didMove(toParentViewController: vc) //for someone, may need to do this. //self.navigationController?.addChildViewController(vc) //self.navigationController?.didMove(toParentViewController: vc) }
现在在
SecondViewController
当你想回去
//Obj-C - (void) goBack { [self removeFromParentViewController]; } //Swift func goBack() { self.removeFromParentViewController() }
玩得好(每个场景):)
是的,这不会显示一个animation,在我的情况下,我显示了一个自定义popup内部vc
虽然它看起来不错,这个代码!