ios 以编程方式将 ViewController 置于其他之上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28929424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Present ViewController on top of other programmatically
提问by Dor Cohen
I have the following code which present my viewcontroller insteadof an existing one:
我有以下代码显示我的视图控制器而不是现有的:
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 topof the other one programmatically,
但我想以编程方式将视图控制器呈现在另一个之上,
any ideas?
有任何想法吗?
回答by Nikita Kukushkin
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
.
如果你希望你的模态视图控制器是可见的,你必须给它的视图一个clear color
背景。并将其设置modalPresentationStyle
为.OverCurrentContext
.
let vc = storyBoard.instantiateInitialViewController() as! MyPresenterViewController
vc.view.backgroundColor = .clearColor()
vc.modalPresentationStyle = .OverCurrentContext
viewControllerToPresentIn.presentViewController(vc, animated: true, completion: nil)
回答by Dor Cohen
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)
回答by Thellimist
If your using navigationcontroller you can change viewcontrollers as
如果您使用导航控制器,您可以将视图控制器更改为
navigationController?.presentViewController(newVC, animated: true, completion: nil)
but since presenting doesn't bring the view from left or right you'll lose your navigationcontroller
但由于呈现不会从左或右带来视图,你会失去你的导航控制器