ios 呈现模态视图控制器隐藏导航栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7034033/
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
Presenting a Modal View Controller hides the Navigation Bar
提问by EmphaticArmPump
I have a navigation based app with a navigation bar, but there are a few instances where instead of pushing a view controller onto the stack, I need to present the view controller modally. The problem is that when I dismiss the modal view controller, everything functions as expected except that the navigation bar is hidden and the (parent view) has been resized, which is the expected behavior according to the docs. So I figured I could simply call a built-in method to unhide the navigation bar. I have already tried
我有一个带有导航栏的基于导航的应用程序,但在某些情况下,我需要以模态方式呈现视图控制器,而不是将视图控制器推送到堆栈上。问题是,当我关闭模态视图控制器时,除了导航栏被隐藏并且(父视图)已调整大小外,一切都按预期运行,这是根据文档的预期行为。所以我想我可以简单地调用一个内置方法来取消隐藏导航栏。我已经试过了
[self.navigationController setNavigationBarHidden:NO];
as well as the animated version without success.
以及没有成功的动画版本。
The documentation talks about this in the method
文档在方法中讨论了这一点
presentModalViewController: animated:
in the discussion section where it says,
在它说的讨论部分,
On iPhone and iPod touch devices, the view of modalViewController is always presented full screen" and "Sets the modalViewController property to the specified view controller. Resizes its view and attaches it to the view hierarchy."However, the docs didn't clue me in as to how to undo this process after dismissing a modal view.
在 iPhone 和 iPod touch 设备上,modalViewController 的视图总是全屏显示”和“将 modalViewController 属性设置为指定的视图控制器。调整其视图的大小并将其附加到视图层次结构。”但是,文档并没有告诉我如何在解除模态视图后撤消此过程。
Has anyone else experienced this and found a solution?
有没有其他人经历过这个并找到了解决方案?
Edit: I am having this same problem, so instead of asking my own question I am sponsoring a bounty on this one. This is my specific situation:
编辑:我遇到了同样的问题,所以我没有问自己的问题,而是赞助了这个问题的赏金。这是我的具体情况:
In my case, I am presenting an Image Picker in a Modal View Controller, over a Navigation Controller:
就我而言,我在模态视图控制器中通过导航控制器呈现图像选择器:
-(void) chooseImage {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
imagepicker = [[UIImagePickerController alloc] init];
imagepicker.allowsEditing = NO;
imagepicker.delegate = self;
imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagepicker.navigationBar.opaque = true;
imagepicker.wantsFullScreenLayout = NO;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if (self.view.window != nil) {
popoverController = [[UIPopoverController alloc] initWithContentViewController:imagepicker];
[popoverController presentPopoverFromBarButtonItem:reset permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
} else {}
} else {
[self.navigationController presentModalViewController:imagepicker animated:YES];
}
}
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[self.popoverController dismissPopoverAnimated:true];
} else {
[self.navigationController dismissModalViewControllerAnimated:YES];
}
//Save the image
}
-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[self.popoverController dismissPopoverAnimated:true];
} else {
[self.navigationController dismissModalViewControllerAnimated:YES];
}
}
回答by Michael Frederick
Make sure you a presenting AND dismissing the modalViewController from the UINavigationController, like so:
确保您从 UINavigationController 呈现和关闭 modalViewController,如下所示:
// show
[self.navigationController presentModalViewController:vc animated:YES];
// dismiss
[self.navigationController dismissModalViewControllerAnimated:YES];
If your view controller is actually on the UINavigationController's stack then this is the correct way to handle the presentation and dismissal of the modal view controller. If your UINavigationBar is still hidden, there is something else funky going on and we would need to see your code to determine what is happening.
如果您的视图控制器实际上在 UINavigationController 的堆栈上,那么这是处理模态视图控制器的呈现和解除的正确方法。如果您的 UINavigationBar 仍然隐藏,那么还有其他一些奇怪的事情发生,我们需要查看您的代码以确定发生了什么。
Edit
编辑
I copied your code into an app of mine and the UIImagePickerController successfully presented and dismissed and my UINavigationController's UINavigationBar was still there. I truly believe that the problem lays elsewhere in your architecture. If you upload a zip w/ an example project I will take a look.
我将您的代码复制到我的应用程序中,并且 UIImagePickerController 成功呈现和解除,而我的 UINavigationController 的 UINavigationBar 仍然存在。我真的相信问题出在您的架构中的其他地方。如果您上传带有示例项目的 zip,我会看一下。
回答by Priyank Ranka
Simply try following code it will work
只需尝试以下代码即可
SettingsViewController *settings = [[SettingsViewController alloc] init];
UINavigationController *navcont = [[UINavigationController alloc] initWithRootViewController:settings];
[self presentModalViewController:navcont animated:YES];
[settings release];
[navcont release];
One need to present the navigation controller in order to have navigation bar on the presented controller
需要呈现导航控制器才能在呈现的控制器上显示导航栏
回答by Bob Spryn
I think I've seen this behavior when presenting a view controller on the wrong VC. Are you calling presentModalViewController
on the navigation controller or the individual VC?
我想我在错误的 VC 上呈现视图控制器时已经看到了这种行为。你是在调用presentModalViewController
导航控制器还是单个 VC?
Try calling it from the navigationController if you aren't already.
如果您还没有,请尝试从 navigationController 调用它。
[self.navigationController presentModalViewController:myVC animated:YES];
回答by Asad ali
AddContactVC *addController =[self.storyboard instantiateViewControllerWithIdentifier:@"AddContactVC"];
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:addController];
[self presentViewController:navigationController animated:YES completion: nil];
working for me shows navigation bar
为我工作显示导航栏
回答by Rajesh
If you present a controller as model, View controller will appear to total view.
如果您将控制器作为模型呈现,则视图控制器将出现在总视图中。
If you want to access the navigation controller properties over the model view, You need to create another navigation controller reference and it continues as previous.
如果您想通过模型视图访问导航控制器属性,您需要创建另一个导航控制器引用,它会像以前一样继续。
This may be useful for you.
这可能对您有用。
回答by Parth Bhatt
Check this out. This is Apple's Documentation under UIViewController Class Reference:
看一下这个。这是 UIViewController 类参考下的 Apple 文档:
It clearly mentions that modal view always presents in full screen mode, so it is obvious that navigation bar will be hidden. So put the seperate navigation bar on modal view to navigate back.
它清楚地提到模态视图总是以全屏模式呈现,因此很明显导航栏将被隐藏。因此,将单独的导航栏放在模态视图上以导航回来。
presentModalViewController:animated:
Presents a modal view managed by the given view controller to the user.
- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
Parameters
modalViewController
The view controller that manages the modal view.
animated
If YES, animates the view as it's presented; otherwise, does not.
Discussion
On iPhone and iPod touch devices, the view of modalViewController is always presented full screen. On iPad, the presentation depends on the value in the modalPresentationStyle property.
Sets the modalViewController property to the specified view controller. Resizes its view and attaches it to the view hierarchy. The view is animated according to the transition style specified in the modalTransitionStyle property of the controller in the modalViewController parameter.
Availability
Available in iOS 2.0 and later.
Hope this helps you understand that hiding the whole view along with navigation controller is default behaviour for modal view so try putting a seperate navigation bar in modal view to navigate.
希望这可以帮助您了解隐藏整个视图和导航控制器是模态视图的默认行为,因此请尝试在模态视图中放置一个单独的导航栏进行导航。
You can check it further on this link
您可以在此链接上进一步检查
回答by ravron
Emphatic and Devin –
强调和德文 -
As I started reading through the Apple docs to get familiar with the problem, I noticed that the method you're using, presentModalViewController:animated:
, appears to be deprecated in favor of presentViewController:animated:completion:
. Perhaps you should try to use that method instead.
当我开始通读 Apple 文档以熟悉该问题时,我注意到您使用的方法presentModalViewController:animated:
, 似乎已被弃用而支持presentViewController:animated:completion:
. 也许您应该尝试使用该方法。
For your convenience, take a look for yourself:
为方便起见,请自行查看:
presentModalViewController:animated: reference
presentModalViewController:animated: 参考
I'll try to put together a quick test program to see whether what I've said above is actually true. But give it a shot – maybe it'll help!
我将尝试编写一个快速测试程序,以查看我上面所说的是否属实。但是试一试——也许它会有所帮助!
回答by john.k.doe
Xcode has a template that does pretty close to what you're doing. from the results, i don't think you should be attempting to perform [self.navigationController presentModalViewController:vc] and [self.navigationController dismissModalViewControllerAnimated:] , but rather simply [self presentModalViewController:] and [self dismissModalViewControllerAnimated:] .
Xcode 有一个模板,它与您正在做的事情非常接近。从结果来看,我认为您不应该尝试执行 [self.navigationController presentModalViewController:vc] 和 [self.navigationController disconnectModalViewControllerAnimated:] ,而只是简单地执行 [self presentModalViewController:] 和 [self deniedModalViewControllerAnimated:] 。
to see how the template does this for yourself, you can use the new project wizard in xcode 4.3 . perhaps it will provide some guidance:
要查看模板如何为自己执行此操作,您可以使用 xcode 4.3 中的新项目向导。也许它会提供一些指导:
from that choice, choose Next, then give your test project a name, choose "Universal", turn off automatic reference counting, hit next, save where you want it.
从该选项中,选择 Next,然后为您的测试项目命名,选择“Universal”,关闭自动引用计数,点击下一步,将其保存在您想要的位置。
now, click on the target and switch the deployment target to 4.3 (or 4.0 if you prefer) for your testing purposes, and switch to your device or the iOS 4.3 simulator .
现在,单击目标并将部署目标切换到 4.3(或 4.0,如果您愿意)以进行测试,然后切换到您的设备或 iOS 4.3 模拟器。
finally, substitute the following code in applicationDidFinishLaunching:withOptions: in the created AppDelegate.m:
最后,在创建的 AppDelegate.m 中的 applicationDidFinishLaunching:withOptions: 中替换以下代码:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.mainViewController = [[[MainViewController alloc] initWithNibName:@"MainViewController_iPhone"
bundle:nil] autorelease];
} else {
self.mainViewController = [[[MainViewController alloc] initWithNibName:@"MainViewController_iPad"
bundle:nil] autorelease];
}
UINavigationController* navigationController
= [[UINavigationController alloc] initWithRootViewController:self.mainViewController];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
now, when i run this, it doesn't hide the navigationBar. and in the created MainViewController.m from the template, you'll see how it presents the modal view controller and dismisses it from the controller itself and not from the navigation controller. for good measure, to make the template code more like your own, go into MainViewController.m and delete the line that sets the modal view controller transition style ...
现在,当我运行它时,它不会隐藏导航栏。在从模板创建的 MainViewController.m 中,您将看到它如何呈现模态视图控制器并将其从控制器本身而不是从导航控制器中解除。为了更好地衡量,为了使模板代码更像您自己的代码,请进入 MainViewController.m 并删除设置模态视图控制器过渡样式的行...
(of course, in iOS 5, with storyboards, the same thing can all be accomplished with modal segues ... which is how i've done this for apps that i'm not supporting for pre-5.0 that present a modalViewController in this fashion.)
(当然,在 iOS 5 中,有了故事板,同样的事情都可以用模态转场来完成......这就是我对我不支持的 5.0 之前的应用程序这样做的方式,这些应用程序在这个时尚。)
回答by Abo3atef
One of the best solution it to use this Category MaryPopin https://github.com/Backelite/MaryPopin
使用此类别 MaryPopin https://github.com/Backelite/MaryPopin的最佳解决方案之一