我想以编程方式呈现一个 UIViewController
,它应该显示(或不显示)透明背景。我想要它适用于 iOS 7.0 及更高版本。我发现自己有很多问题(和答案),但他们无法帮助我。这是我的应用的 View 层次结构。
我正在使用侧边菜单 Controller (RESideMenu)。
我有一个 rootView(来自 RESideMenu)-> 在 UINavigationController
中显示一个中心 Controller (以及一个左 View Controller )。
在需求中,我想呈现一个 View Controller
From a pushed view controller (in navigational hierarchy)
From a presented view controller (in navigational hierarchy)
此外,我需要呈现它并执行一些操作,然后将其删除。
我很确定这在很多情况下都可以工作,有(或没有)侧边菜单,甚至是导航 Controller 。
我正在将一个单独的问题(当然还有它的答案)发布到这个队列中,因为我认为这对社区开发人员可能会有所帮助,他们可能也因缺乏针对此问题的可接受的解决方案而感到沮丧。
最佳答案
Suppose, we're in
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)
}
Some of you may need to write above method like this,
//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)
}
Now in
SecondViewController
when you want to go back
//Obj-C
- (void) goBack {
[self removeFromParentViewController];
}
//Swift
func goBack() {
self.removeFromParentViewController()
}
玩好(每个场景):)
是的,这不会显示动画,在我的例子中,我在 vc
中显示自定义弹出窗口,尽管这段代码看起来不错!
关于ios - 呈现具有透明背景的模态视图 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27598846/