呈现具有透明背景的模态视图控制器 [英] Present a modal view controller with transparent background

查看:105
本文介绍了呈现具有透明背景的模态视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to present a UIViewController programmatically which should show up (or not, rather) with transparent background. I want it for iOS 7.0 and above. I've find myself many questions (and answers) but they weren't able to help me. Here's the view hierarchy of my app.

我正在使用侧边菜单控制器(RESideMenu)。

I'm using a side menu controller (RESideMenu).

我有一个rootView(来自RESideMenu) - >在 UINavigationController

I have a rootView (base from RESideMenu) -> Showing a Center controller (along with a left view controller) in UINavigationController.

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

In requirements, I'd like to present a view controller


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

From a pushed view controller (in navigational hierarchy)

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

From a presented view controller (in navigational hierarchy)

此外,我需要展示它并执行一些操作,然后将其删除。

In addition, I need to present it and perform some action, and then remove it.

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

I'm pretty sure that this should work in many cases, with (or without) side menu, or even navigation controller.

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

I'm posting a separate question (and of course its answer too) into this queue, because I think it would prove helpful to the community devs who might also have been frustrated by the lack of an acceptable solution to this problem.

推荐答案


假设,我们在 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)
}




现在在 SecondViewController 当你想回去时



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

//Swift
func goBack() {
    self.removeFromParentViewController()
}

玩得好(每个场景):))

Do play well (with each scenario) :)

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

And yes, this will not show an animation, in my case, I'm showing a custom popup inside vc though it looks nice with this code!

这篇关于呈现具有透明背景的模态视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆