ios 模态转场,导航栏消失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15689261/
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
Modal segue, navigation bar disappears
提问by Johnny Dahdah
I'm using Xcode 4.6.1 to code on Objective-C. I want to know how can I keep the navigation bar shown when I create a modal segue between 2 View controllers, because I'm doing the segue in the storyboard and when I run the application the navigation bar of the second view controller disappears, and I have a done button on that bar but I can't see it.
我使用 Xcode 4.6.1 在 Objective-C 上编码。我想知道当我在 2 个视图控制器之间创建模态转场时如何保持显示导航栏,因为我在故事板中进行转场,并且当我运行应用程序时,第二个视图控制器的导航栏消失了,并且我在那个栏上有一个完成按钮,但我看不到它。
回答by Saleh Masum
Just add another Navigation Controller
to your modal view controller. Follow the steps
只需将另一个添加Navigation Controller
到您的模态视图控制器。按照步骤
- Select the
Modal View Controller
- Go to
Editor menu
- Select
Embed In
- Select
Navigation Controller
- 选择
Modal View Controller
- 去
Editor menu
- 选择
Embed In
- 选择
Navigation Controller
Run the application. Now it should work perfectly.
运行应用程序。现在它应该可以完美运行。
Hope this solves your problem.
希望这能解决您的问题。
回答by rdelmar
Modal segues take over the whole screen, so any navigation bars, tool bars, or tab bars that are in the presenting controller will be covered up. If you want a navigation bar on this modal controller, you'll need to add one specifically to it, and add any buttons you want to that new navigation bar (or tool bar). If you don't want to do this, then don't present it modally, do a push to it.
Modal segues 占据了整个屏幕,因此呈现控制器中的任何导航栏、工具栏或标签栏都将被覆盖。如果你想在这个模态控制器上有一个导航栏,你需要专门为它添加一个导航栏,并将你想要的任何按钮添加到新的导航栏(或工具栏)。如果你不想这样做,那么不要模态地呈现它,推动它。
回答by Christopher Smit
You could simply do the following to show the navigation bar:
您可以简单地执行以下操作来显示导航栏:
Objective-C
目标-C
UINavigationController alloc]initWithRootViewController:modalVC];
SWIFT 3
斯威夫特 3
let modelVC = self.storyboard?.instantiateViewController(withIdentifier: "modalVC") as! ModalVC
let navBarOnModal: UINavigationController = UINavigationController(rootViewController: modalVC)
self.present(navBarOnModal, animated: true, completion: nil)
This will show the modal view controller with the navigation bar. Knowledge on Objective-C is limited, so I did not write the part of presenting. You should be able to figure that one out. ;)
这将显示带有导航栏的模态视图控制器。Objective-C的知识有限,所以我没有写presentation部分。你应该能够弄清楚这一点。;)
Hope this helps!
希望这可以帮助!
回答by user1760527
In iOS 8 there is a better method. You can use adaptive presentation styles:
在 iOS 8 中有一个更好的方法。您可以使用自适应演示样式:
- Use a "Present as popover" segue
- Set your controller to adopt the UIPopoverPresentationControllerDelegate protocol
- Implement 2 methods
- 使用“Present as popover”segue
- 将您的控制器设置为采用 UIPopoverPresentationControllerDelegate 协议
- 实现2个方法
Objective-C:
目标-C:
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationFullScreen;
}
- (UIViewController *)presentationController:(UIPresentationController *)controller viewControllerForAdaptivePresentationStyle:(UIModalPresentationStyle)style {
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: controller.presentedViewController];
return navController;
}
Swift:
迅速:
UIPopoverPresentationControllerDelegate
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.FullScreen
}
func presentationController(controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
var navController = UINavigationController(rootViewController: controller.presentedViewController)
return navController
}
Swift 4:
斯威夫特 4:
extension MyViewController: UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.fullScreen
}
func presentationController(_ controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
return UINavigationController(rootViewController: controller.presentedViewController)
}
}
In prepare for segue method set the delegate:
在准备转场方法中设置委托:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if let adpostVC = segue.destinationViewController as? XXXController {
let popPC = adpostVC.popoverPresentationController
popPC?.delegate = self
回答by Volodymyr Nazarkevych
swift version:
快速版本:
Follow steps:
请按照以下步骤操作:
- Embed VC in NavigationController
Override prepareForSegue()
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "goToYourController" { let navigation: UINavigationController = segue.destinationViewController as! UINavigationController var vc = YourViewController.init() vc = navigation.viewControllers[0] as! YourViewController //if you need send something to destnation View Controller //vc.delegate = self } }
- 在 NavigationController 中嵌入 VC
覆盖 prepareForSegue()
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "goToYourController" { let navigation: UINavigationController = segue.destinationViewController as! UINavigationController var vc = YourViewController.init() vc = navigation.viewControllers[0] as! YourViewController //if you need send something to destnation View Controller //vc.delegate = self } }
回答by jungledev
There is an easier way to do this. Like previous comments said (but didn't explain all the steps) you need to embed your desired destination view controller in a navigation controller, then set your destination view controller to be the Navigation Controller, then the Navigation controller calls the destination view controller.
有一种更简单的方法可以做到这一点。就像之前的评论所说(但没有解释所有步骤),您需要将所需的目标视图控制器嵌入到导航控制器中,然后将目标视图控制器设置为导航控制器,然后导航控制器调用目标视图控制器。
First, embed the VC in a NavVC. Then you must write the code to set the segue destination to be the Nav VC.
首先,将 VC 嵌入到 NavVC 中。然后您必须编写代码以将 segue 目标设置为 Nav VC。
The code looks like this:
代码如下所示:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UINavigationController *nav = segue.destinationViewController;
AddTriviaVC *triv = [[AddTriviaVC alloc]init];
triv = nav.viewControllers[0];
triv.location = self.location;
}
I hope this makes sense.
我希望这是有道理的。
回答by Levi
That is probably because you don't have a UINavigationController
in your modal. You should use one (or just add a navigation bar to your ViewController in the Storyboard), and present that modally.
那可能是因为您UINavigationController
的模态中没有 a 。您应该使用一个(或者只是在 Storyboard 的 ViewController 中添加一个导航栏),并以模态方式呈现。
回答by Stan Cromlish
You can add a toolbar programatically by doing the following in -(void)viewDidLoad
您可以通过执行以下操作以编程方式添加工具栏 -(void)viewDidLoad
NSInteger tbHeight = 50;
tb = [[UIToolbar alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - tbHeight), self.view.frame.size.width, tbHeight)];
tb.translucent = YES;
emailButton = [[UIBarButtonItem alloc] initWithTitle:@"Email Results" style:UIBarButtonItemStyleBordered target:tvController action:@selector(actionSheet:)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(doneButtonPressed:)];
NSArray *barButton = [[NSArray alloc] initWithObjects:emailButton,flexibleSpace,doneButton,nil];
[tb setItems:barButton];
[self.view addSubview:tb];
barButton = nil;
You will then have to create an IBAction for pressing the done button and it is done just like this:
然后,您必须创建一个 IBAction 来按下完成按钮,就像这样完成:
-(IBAction)doneButtonPressed:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
That should give you what you want with your modal view controller.
这应该给你你想要的模式视图控制器。
回答by LE24
in storyboard , you should add a Navigation Itemto your new viewController, then add Bar Button Itemfor your done button
在 storyboard 中,你应该向你的新 viewController添加一个导航项,然后为你的完成按钮添加Bar Button Item
回答by Alex D.
Here is my SHORTED version of "Volodymyr Nazarkevych" in Swift 4.2
这是我在 Swift 4.2 中的“Volodymyr Nazarkevych”的简短版本
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
switch(segue.identifier ?? "") {
case "segueIdentifier": // name of your segue
let navigation: UINavigationController = segue.destination as! UINavigationController
//here you create new name of your destination ViewController and assign his name to let constant above
guard let myNewViewController = navigation.viewControllers[0] as? DestinationViewController
//DestinationViewController is your 2-ViewController where you go to from first one.
else {
fatalError("Unexpected destination: \(segue.destination)")
}
// TO DO SOMETHING HERE
default:
fatalError("Unexpected Segue Identifier; \(String(describing: segue.identifier))")
}
}
The code of "Volodymyr Nazarkevych" works perfectly, but only when your segue is from 1-ViewController to NavigationController of 2-ViewController, not directly to 2-ViewController. THANKS A LOT VOVA!.
“Volodymyr Nazarkevych”的代码完美运行,但只有当你的segue是从1-ViewController到2-ViewController的NavigationController,而不是直接到2-ViewController。非常感谢 VOVA!
Also in switch cases after destination block code you can do different stuff, for example to get some information or file from second ViewController.
同样在目标块代码之后的切换案例中,您可以做不同的事情,例如从第二个 ViewController 获取一些信息或文件。