以编程方式呈现ViewController

Dor*_*hen 13 ios swift

我有以下代码,它呈现我的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)
Run Code Online (Sandbox Code Playgroud)

但我想以编程方式将viewcontroller呈现在另一个上面,

有任何想法吗?

Nik*_*kin 38

如果您希望查看模态视图控制器,则必须为其视图提供clear color背景.并设置modalPresentationStyle.OverCurrentContext.

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

viewControllerToPresentIn.presentViewController(vc, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)


Dor*_*hen 6

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

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