呈现具有透明背景的模态视图控制器

Hem*_*ang 9 uiviewcontroller uinavigationcontroller ios presentviewcontroller

我想以UIViewController编程方式呈现一个应该以透明背景显示(或不显示)的内容.我希望它适用于iOS 7.0及更高版本.我发现自己有很多问题(和答案),但他们无法帮助我.这是我的应用程序的视图层次结构.

我正在使用侧面菜单控制器(RESideMenu).

我有一个rootView(来自RESideMenu的基础) - >显示一个中心控制器(以及一个左视图控制器)UINavigationController.

在要求中,我想呈现一个视图控制器

从推动的视图控制器(在导航层次结构中)

从呈现的视图控制器(在导航层次结构中)

另外,我需要呈现它并执行一些操作,然后将其删除.

我很确定这应该适用于许多情况,有(或没有)侧面菜单,甚至导航控制器.

我在这个队列中发布了一个单独的问题(当然也是它的答案),因为我认为这对社区开发者有帮助,他们可能也因为缺乏可接受的解决方案而感到沮丧.

Hem*_*ang 14

假设,我们在 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)
}
Run Code Online (Sandbox Code Playgroud)

有些人可能需要像这样写上面的方法,

//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)
}
Run Code Online (Sandbox Code Playgroud)

现在,SecondViewController当你想回去

//Obj-C
- (void) goBack {
    [self removeFromParentViewController];
}

//Swift
func goBack() {
    self.removeFromParentViewController()
}
Run Code Online (Sandbox Code Playgroud)

玩得好(每个场景):)

是的,这不会显示动画,在我的情况下,我正在显示一个自定义弹出窗口vc虽然看起来很好用这个代码!

  • 致,**亲爱的Downvoters**,请尝试考虑将您的足迹留在这里,并评论您投票的原因.学习一些东西并改善自我是非常有帮助的.我在这里保留我的答案(如我的问题所示)来帮助你(和其他开发者一样).但是,如果它看起来很糟糕或者不适合你,那么随意写下你的话(如果可能的话,你的解决方案也是如此)以及你不同意的投票.我一定很感激,谢谢!:) (23认同)