以编程方式在其他视图控制器之上显示视图控制器

时间:2022-04-04 15:00:42

I have the following code which present my viewcontroller instead of an existing one:

我有以下代码显示我的viewcontroller而不是现有的:

let frameworkBundle = NSBundle(identifier: "me.app.MyFramework")
var storyBoard:UIStoryboard = UIStoryboard(name: "MyStoryboard", bundle: frameworkBundle)

var vc = storyBoard.instantiateInitialViewController() as MyPresenterViewController

viewControllerToPresentIn.presentViewController(vc, animated: true, completion: nil)

But I want to present the viewcontroller on top of the other one programmatically,

但是我想以编程的方式在另一个视图控制器的顶部显示视图控制器,

any ideas?

什么好主意吗?

3 个解决方案

#1


28  

If you want your modal view controller to be see-though you have to give its view a clear color background. And set its modalPresentationStyle to .OverCurrentContext.

如果你想让你的模态视图控制器是透明的——尽管你必须给它的视图一个清晰的颜色背景。并将其modalPresentationStyle设置为. overcurrentcontext。

let vc = storyBoard.instantiateInitialViewController() as! MyPresenterViewController
vc.view.backgroundColor = .clearColor()
vc.modalPresentationStyle = .OverCurrentContext

viewControllerToPresentIn.presentViewController(vc, animated: true, completion: nil)

#2


5  

I used the following approach in order to present my view only on the bottom of the other view controller:

我使用下面的方法,只在另一个视图控制器的底部显示我的视图:

var presenterStoryBoard:UIStoryboard = UIStoryboard(name: "MiniAppPanel", bundle: frameworkBundle)

var vc = presenterStoryBoard.instantiateInitialViewController() as MiniAppPanelViewController

vc.view.frame = CGRectMake(0, vc.view.frame.size.height - 120, vc.view.frame.size.width, 120)

publisherViewController.addChildViewController(vc)    
publisherViewController.view.addSubview(vc.view)

#3


0  

If your using navigationcontroller you can change viewcontrollers as

如果您使用的是navigationcontroller,那么您可以将viewcontrolleras更改为

    navigationController?.presentViewController(newVC, animated: true, completion: nil)

but since presenting doesn't bring the view from left or right you'll lose your navigationcontroller

但是由于呈现不能从左或右带来视图,您将丢失导航控制器

#1


28  

If you want your modal view controller to be see-though you have to give its view a clear color background. And set its modalPresentationStyle to .OverCurrentContext.

如果你想让你的模态视图控制器是透明的——尽管你必须给它的视图一个清晰的颜色背景。并将其modalPresentationStyle设置为. overcurrentcontext。

let vc = storyBoard.instantiateInitialViewController() as! MyPresenterViewController
vc.view.backgroundColor = .clearColor()
vc.modalPresentationStyle = .OverCurrentContext

viewControllerToPresentIn.presentViewController(vc, animated: true, completion: nil)

#2


5  

I used the following approach in order to present my view only on the bottom of the other view controller:

我使用下面的方法,只在另一个视图控制器的底部显示我的视图:

var presenterStoryBoard:UIStoryboard = UIStoryboard(name: "MiniAppPanel", bundle: frameworkBundle)

var vc = presenterStoryBoard.instantiateInitialViewController() as MiniAppPanelViewController

vc.view.frame = CGRectMake(0, vc.view.frame.size.height - 120, vc.view.frame.size.width, 120)

publisherViewController.addChildViewController(vc)    
publisherViewController.view.addSubview(vc.view)

#3


0  

If your using navigationcontroller you can change viewcontrollers as

如果您使用的是navigationcontroller,那么您可以将viewcontrolleras更改为

    navigationController?.presentViewController(newVC, animated: true, completion: nil)

but since presenting doesn't bring the view from left or right you'll lose your navigationcontroller

但是由于呈现不能从左或右带来视图,您将丢失导航控制器