How to hide status bar in UIImagepickercontroller?

23,484

Solution 1

I had an issue where in iOS7 my status bar was not being hidden. I hid it programmatically and it still displayed in iOS7, but when ran in iOS6 the status bar would hide appropriately. You have to go to the plist and add the following:

'view controller-based status bar appearance' and set to NO.

If you want the status bar to re-appear in other view controllers and only be hidden on a particular VC, then you set the status bar to hidden YES when the VC loads. When the VC will disappear you set the status bar hidden back to NO.

- (void)viewDidLoad
{

    [super viewDidLoad];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];

}

and when the controller will disappear you add the following to set the status bar so it is no longer hidden and will display on the next View:

-(void)viewWillDisappear:(BOOL)animated{

     [[UIApplication sharedApplication] setStatusBarHidden:NO];

}

setStatusBarHidden:withAnimation: if you want some smooth animation

Solution 2

This worked fine for me:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
}

Edit: As of today i just found out that in your info.plist, if you just copy-paste view controller-based status bar appearance there it won't work ... you have to hit enter on a property, and scroll to the last one of them so you will have autocomplete to :view controller-based status bar appearance and an boolean, with no. I tried multiple times but it does not work just copying. Have a nice day.

Solution 3

The solution I found for applications build around : "View controller-based status bar appearance" set to YES

I did add Category:

//UIImagePickerController+StatusBarHidden.h
#import <UIKit/UIKit.h>

@interface UIImagePickerController (StatusBarHidden)
@end

//UIImagePickerController+StatusBarHidden.h
#import "UIImagePickerController+StatusBarHidden.h"

@implementation UIImagePickerController (StatusBarHidden)

-(BOOL) prefersStatusBarHidden {
    return YES;
}

-(UIViewController *) childViewControllerForStatusBarHidden {
    return nil;
}

@end

The method childViewControllerForStatusBarHidden is used rarely, but image picker do use it, thats why might cause some troubles

You may also implement UIViewController singleton, with method which returns YES or NO, based on its property. Then your View controleller implements childViewControllerForStatusBarHidden returning the above singleton. Changing singleton property automatically change statusbar in app. There also is twin method childViewControllerForStatusBarStyle


However for 2014, iOS8, see this https://stackoverflow.com/a/18960308/294884

Solution 4

subclass UIImagePickerController ... mine is V1ImagePickerController ...

.m file looks like this:

#import "V1ImagePickerController.h"

@interface V1ImagePickerController ()

@end

@implementation V1ImagePickerController

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)   // iOS7+ only
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;

        [self prefersStatusBarHidden];
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    }
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (UIViewController *)childViewControllerForStatusBarHidden
{
    return nil;
}

@end

the childViewControllerForStatusBarHidden is the key!

Solution 5

If you want to disable the status bar from plist, try this:

  1. Status bar is initially hidden : YES
  2. View controller-based status bar appearance : NO

this is necessary for iOS 7, works for me. I do not know if there are some other techniques for doing this in iOS7. Set these two tags in your info.plist.

Everytime your viewcontroller appears, in viewDidLoad or when image picker controller finishes , use this:

 - (void) imagePickerController:(UIImagePickerController *)picker1 didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
   [[UIApplication sharedApplication] setStatusBarHidden:YES];
 .
 .
 .
 .
 }
Share:
23,484
Shailendra Kumar Gangwar
Author by

Shailendra Kumar Gangwar

Updated on April 15, 2020

Comments

  • Shailendra Kumar Gangwar
    Shailendra Kumar Gangwar about 4 years

    I am new to iOS development. I am trying to hide status bar in UIImagePickerController. Whenever I click on "Take photo", status bar appears. It doesn't hide. I want status bar to be hidden only in UIImagePickerController.

    Here is my code,

    - (IBAction)takePhoto:(UIButton *)sender
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:picker animated:YES completion:NULL];
    }
    
    
    - (void)imagePickerController:(UIImagePickerController *)picker     didFinishPickingMediaWithInfo:(NSDictionary *)info
    {   
        [self statusBar:YES];
        UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
        self.imageView.image = chosenImage;
        [picker dismissViewControllerAnimated:YES completion:NULL];
    
    }
    
    
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
    {
    [picker dismissViewControllerAnimated:YES completion:NULL];
    }
    
    
    -(void)statusBar:(BOOL)status
    {
        [[UIApplication sharedApplication] setStatusBarHidden:status];
    }
    

    How to hide the status bar on UIImagePickerController?

  • Shailendra Kumar Gangwar
    Shailendra Kumar Gangwar almost 11 years
    it doesn't work for me. Status bar appears whenever i click on "Take Photo".
  • Shailendra Kumar Gangwar
    Shailendra Kumar Gangwar almost 11 years
    I just tried this code. In view controller, status bar gets hide.But in UIImagePickerController, it appears again.
  • D-eptdeveloper
    D-eptdeveloper almost 11 years
    okay i am editing my answer according to your question now please try in this way and remove other codes you've done
  • Shailendra Kumar Gangwar
    Shailendra Kumar Gangwar almost 11 years
    Yeah Sure. It will be a great help.
  • D-eptdeveloper
    D-eptdeveloper almost 11 years
    @ShailendraKrGangwar : try my edited code and make sure you've not calling statusbar method anywhere else.
  • D-eptdeveloper
    D-eptdeveloper almost 11 years
    Don't know what is missing now only way is if you could share your project so that i can take a look what is wrong or paste your full viewController.m file in your question.
  • Shailendra Kumar Gangwar
    Shailendra Kumar Gangwar almost 11 years
    I want to display it all through my app. I just want to hide it in UIImagepickerController.
  • Shailendra Kumar Gangwar
    Shailendra Kumar Gangwar almost 11 years
    I had set 'view controller-based status bar appearance' to NO in pList and [application setStatusBarHidden:NO]; [application setStatusBarStyle:UIStatusBarStyleDefault]; in appDelegate. This worked for me. Thanks.
  • RockandRoll
    RockandRoll over 10 years
    i am facing same issue.. but setting view controller-based status bar to NO, hide status bar, but if u browse photo it is coming again.
  • akin
    akin over 10 years
    This works perfectly for iOS7,But when pop the photo view, the new photo is on the bottom, the user had to scroll it at the bottom
  • ngzhongcai
    ngzhongcai over 10 years
    -(BOOL)prefersStatusBarHidden { return self.sourceType== UIImagePickerControllerSourceTypeCamera ? YES : NO; }
  • cloudsurfin
    cloudsurfin over 10 years
    The documentation says subclassing isn't supported. So this is a nice option. Worked great (once I set view controller-based status bar appearance back to YES). Thanks.
  • xarly
    xarly over 10 years
    With the 'view controller-based status bar appearance' and set to NO, I had still the problem, but this fixed the problem, thanks!!!
  • Albert Renshaw
    Albert Renshaw over 10 years
    When you use image picker the status bar fades in... but when you are done using it, this code just hides it instantly... I would suggest using the following code to add a fade out animation to your status bar dismissal. [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
  • Albert Renshaw
    Albert Renshaw over 10 years
    Also! Do NOT forget to put that code (either his or mine) in your - (void)imagePickerControllerDidCancel:(UIImagePickerControlle‌​r*)picker { as well! In case the user clicks "cancel" and doesn't pick a photo!
  • sffc
    sffc over 10 years
    Doesn't this turn off the status bar for the entire application once the UIImagePickerController is opened? Or not? I want to only turn it off for the camera view.
  • farhadf
    farhadf over 10 years
    This works for me and the critical piece is settling childViewControllerForStatusBarHidden to return nil. All the other solutions were all-or-none vis-a-vis status bar display.
  • jrturton
    jrturton over 10 years
    You don't need the viewDidLoad override, not for the status bar stuff anyway.
  • Fattie
    Fattie over 10 years
    Also see Masmor's answer here: stackoverflow.com/questions/18856627/… It's the only way to do it.
  • Błażej
    Błażej about 10 years
    You saved my day man! As @farhadf wrote, childViewControllerForStatusBarHidden is the key here.
  • Frederic Adda
    Frederic Adda about 10 years
    This worked for me too ! However, I noticed a little "glitch" when the UIImagePickerController appeared. I found out that if you use the following method instead, it works without a glitch: [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
  • user
    user almost 10 years
    Brilliant. Works perfectly.
  • greenhouse
    greenhouse almost 10 years
    this doesn't solve for uiimagepickercontroller only. however, you can you CATransation blocks every time you load the view instead. [CATransaction begin]; [CATransaction setCompletionBlock:^{ [[UIApplication sharedApplication] setStatusBarHidden:YES]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault]; }]; imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; [CATransaction commit];
  • Hernan Arber
    Hernan Arber over 9 years
    Yes this Solves the Issue! Thanks
  • zpasternack
    zpasternack over 9 years
    Accepted answer did not work for me, but this did +1
  • Poles
    Poles almost 8 years
    Worked like a charm. Thanks.
  • Sam Soffes
    Sam Soffes almost 7 years
    I'd recommend taking this same approach in a subclass instead, but this method definitely works! Thank you!
  • Nobin Thomas
    Nobin Thomas over 6 years
    "setStatusBarHidden" is deprecated on iOS 9.