How to hide status bar in UIImagepickercontroller?

对着背影说爱祢 提交于 2019-12-28 01:56:05

问题


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?


回答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




回答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.




回答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




回答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!




回答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];
 .
 .
 .
 .
 }



回答6:


I used Silvertaurus's answer above, but with a little modification to the prefersStatusBarHidden method that I thought was very helpful:

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

@interface UIImagePickerController (StatusBarHidden)
@end

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

@implementation UIImagePickerController (StatusBarHidden)

-(BOOL) prefersStatusBarHidden {
    if (self.sourceType==UIImagePickerControllerSourceTypeCamera) {
        return YES;
    } else {
        return NO;
    }
}

-(UIViewController *) childViewControllerForStatusBarHidden {
    return nil;
}

@end

This keeps the status bar up for the image picker when the camera is not displayed.




回答7:


Try this

[[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];

Also check this discussion.




回答8:


Please try this.

• Setting a delegate for the UIImagePickerController

• hide the status bar in the delegate's navigationController:didShowViewController:animated: function.

E.G:

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



回答9:


You can do it with a category:

@interface UIImagePickerController (HideStatusBar)

@end


@implementation UIImagePickerController (HideStatusBar)

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (UIViewController *)childViewControllerForStatusBarHidden
{
    return nil;
}

@end

Source: https://gist.github.com/psobko/9493473




回答10:


-(IBAction)takePhoto:(UIButton *)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self statusBar:TRUE];
    [self presentViewController:picker animated:YES completion:NULL];
}


-(void)imagePickerController:(UIImagePickerController *)picker      didFinishPickingMediaWithInfo:(NSDictionary *)info
{        
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.imageView.image = chosenImage;
    [self statusBar:FALSE];
    [picker dismissViewControllerAnimated:YES completion:NULL];
}


-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self statusBar:FALSE];
    [picker dismissViewControllerAnimated:YES completion:NULL];
}


-(void)statusBar:(BOOL)status
{
    [[UIApplication sharedApplication] setStatusBarHidden:status];
}

that might help you to achieve what you want.




回答11:


Try my answer posted here if you want to keep using ViewController-Based Status Bar Appearance.




回答12:


In my case I had to use presentViewController to show UIImagePickerViewController (iOS7).

1- Set View controller-based status bar appearance to NO in .plist 2- Create a category for UIImagePickerController and in viewDidLayoutSubviews:

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
}

3- Added the two following methods to the category:

- (BOOL)prefersStatusBarHidden{
    return YES;
}

- (UIViewController *)childViewControllerForStatusBarHidden{
    return nil;
}

I hope this will help.




回答13:


i think i solved this in a pretty simple way without subclassing and using plist. it only hides for UIImagePickerController.

this example is for bringing up the photo gallary only, but i imagine you can apply it in the same way to anywhere with in uiimagepickercontroller

- (void)showGallary
{
  [CATransaction begin];
  [CATransaction setCompletionBlock:^{
      [[UIApplication sharedApplication] setStatusBarHidden:YES];
      [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  }];

  imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

  [CATransaction commit];
}



回答14:


I wanted to have the status bar hidden all the time, opening the image picker shows it. To hide it again the following worked for me as of iOS 8:

-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:^{
        [[UIApplication sharedApplication] setStatusBarHidden:YES];
    }];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self dismissViewControllerAnimated:YES completion:^{
        [[UIApplication sharedApplication] setStatusBarHidden:YES];
    }];

    // do stuff with photo
}

I have View controller-based status bar appearance set to NO



来源:https://stackoverflow.com/questions/18760710/how-to-hide-status-bar-in-uiimagepickercontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!