Search code examples
iosobjective-cuinavigationcontroller

How to make navgationBar hidden again?


I am developing an App in which facing some difficulties, after searching a lot solutions, but it is still not working.

First, I create an App like this

nav bar hidden

I use the following codes to make the navgationBar hidden in viewWillAppear: method

[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
                                              forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];

But after I take a photo using UIImagePickerController, the navgationBar will appear on the screen,

like the this: nav bar appear

So, how can I make the navgation bar always hidden? Thanks a lot..

=============== Update =============

I also used UIImagePickerController to choose photo from library, while it works well. After I have chosen and back to this viewController, the bar still hide. It's really strange.


Solution

  • If you want hide your navigation Bar once you go back to your viewController after picking image use this two callbacks method

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
       //Your code....
       [self.navigationController.navigationBar setHidden:YES];
    }
    
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
    {
       //Your code....
       [self.navigationController.navigationBar setHidden:YES];
    }
    

    But if you want remove the navigation bar from your UIImagePickerViewController the you should subclass the UIImagePickerViewController and in your custom UIImagePicker something like this

    .h

    #import <UIKit/UIKit.h>
    
    @interface CustomImagePickerController : UIImagePickerController
    
    @end
    

    .m

    #import "CustomImagePickerController.h"
    
    @interface CustomImagePickerController ()
    
    @end
    
    @implementation CustomImagePickerController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        [self.navigationController.navigationBar setHidden:YES];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    

    and then use this class as your UIImagePickerController

    Hope this helps