[英]How to implement a UINavigationController for modal presentation, not for popover, with an adaptive segue
While using a universal storyboard and adaptive segues, how can one implement a Present As Popover segue that will have a navigation bar (with a title and close button) only on iPhone when presented modally, and will not have a navigation controller on iPad when presented as a popover? 在使用通用故事板和自适应segue的同时,如何实现一个Present As Popover segue,它只在iPhone上以模态方式显示导航栏(带标题和关闭按钮),并且在呈现时不会在iPad上安装导航控制器作为一个popover?
I believe the proper setup is to not include a nav controller in the storyboard, control-drag to the new view controller and select a Present As Popover segue. 我认为正确的设置是不在故事板中包含导航控制器,控制 - 拖动到新的视图控制器并选择当前弹出窗口。 Then in prepareForSegue
one will need to create the navigation controller and embed the destination controller in it, then add the title and buttons, but only if it will be presented modally. 然后在prepareForSegue
需要创建导航控制器并在其中嵌入目标控制器,然后添加标题和按钮,但前提是它将以模态方式呈现。 If that approach is correct, how can one do that in code? 如果这种方法是正确的,那怎么能在代码中做到这一点?
Rdelmar is correct, you can't do this in prepareForSegue
as the destination view controller is already set. Rdelmar是正确的,你不能在prepareForSegue
这样做,因为已经设置了目标视图控制器。
In iOS 7 and earlier you'll have to add that navigation controller to the storyboard and you could then have separate segues to the navigation controller and its root view. 在iOS 7及更早版本中,您必须将该导航控制器添加到故事板,然后您可以将单独的segue添加到导航控制器及其根视图。 Then trigger the right segue on depending on whether you want the navigation controller (iPhone) or not (iPad). 然后根据您是否需要导航控制器(iPhone)(iPad)触发右侧segue。
In iOS 8 you can use the new UIAdaptivePresentationControllerDelegate
protocol and then create a navigation controller on the fly where you need it: 在iOS 8中,您可以使用新的UIAdaptivePresentationControllerDelegate
协议,然后在需要的地方动态创建导航控制器:
func presentationController(controller: UIPresentationController!, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController!
{
let presented = controller.presentedViewController
return UINavigationController(rootViewController: presented)
}
To elaborate on this a little, if what you want is a popover on your iPad but a modal sheet with a close button on your iPhone then this is how you do it. 稍微详细说明一下,如果你想要的是你的iPad上有一个弹出框,而是一个带有关闭按钮的模态表,那么你就是这样做的。
In Xcode 6.3 storyboard, you hook up a view controller and designate the segue as a "Present as Popover" 在Xcode 6.3 storyboard中,您连接一个视图控制器并将segue指定为“Present as Popover”
This code should go in the view controller that segues to the popover, not in the popover itself: 此代码应位于视图控制器中,该控制器将分段为弹出框,而不是弹出框本身:
First you set up the popover delegate: 首先设置popover委托:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "myPopoverSegueName") {
let vc = segue.destinationViewController
vc.popoverPresentationController?.delegate = self
return
}
}
Then you add the delegate extension and create the navigation controller / close button on the fly: 然后添加委托扩展并动态创建导航控制器/关闭按钮:
extension myViewController: UIPopoverPresentationControllerDelegate {
func presentationController(controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
let btnDone = UIBarButtonItem(title: "Done", style: .Done, target: self, action: "dismiss")
let nav = UINavigationController(rootViewController: controller.presentedViewController)
nav.topViewController.navigationItem.leftBarButtonItem = btnDone
return nav
}
}
Then you add your dismiss function and you should be good to go: 然后你添加你的解雇功能,你应该好好去:
func dismiss() {
self.dismissViewControllerAnimated(true, completion: nil)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.