繁体   English   中英

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

[英]Present a modal view controller with transparent background

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

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

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

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

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

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

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

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

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

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

玩得好(每个场景):)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM