iPhone - Remove status bar programmatically

2020-02-28 06:08发布

问题:

I have made an app that implements the iPhone's camera. When the user finishes picking their image, the status bar reappears! How would I make sure that the status bar stays hidden?

Here is my code:

-(IBAction)pickImage:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;

picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
background.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

}

If i am doing anything wrong, please point it out! Thanks, Rafee

回答1:

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];

You may opt for another animation style if at all.



回答2:

In iOS 7, there is a method on UIViewController, "prefersStatusBarHidden". To hide the status bar, add this method to your view controller and return YES:

- (BOOL) prefersStatusBarHidden
{
    return YES;
}


回答3:

In this case,We are using 2 steps

In first step: Add in info.plist: "View controller-based status bar appearance" with value "NO"

In Second step: Use/call this code with delegate of UIImagePickerController

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


回答4:

With iOS 7 and later, you can use the following code to hide and unhide the status bar,

@interface ViewController()

@property (nonatomic, getter=isStatusBarHidden) BOOL statusBarHidden;

@end

@implementation ViewController


  ... other codes

- (BOOL)prefersStatusBarHidden {
    return self.isStatusBarHidden;
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    return UIStatusBarAnimationFade;
}

- (void)hideStatusBar {
    self.statusBarHidden = YES;
    [self setNeedsStatusBarAppearanceUpdate];
}

- (void)showStatusBar {
    self.statusBarHidden = NO;
    [self setNeedsStatusBarAppearanceUpdate];
}

@end


回答5:

There seems to be a bug in the dismiss mechanism of UIViewController associated with UIImagePicker, with a sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum.

The moment of the call to dismissModalViewController (plus the method with completion:) the UIApplication's status bar hidden property instantly changes from YES to NO, and it is drawn at the moment of stepping over dismiss...

This is only really obvious for apps that use a full-screen view. My current app project does, plus I control the frame of the view controller's view before presenting, so the UIImagePicker is NOT full screen. This made the bug VERY obvious. I spent 4-5 hours determining the cause, and this was the final certain conclusion, and the bug does NOT occur for sourceType Camera nor PhotoLibrary.

So if you want a perfectly full-screen app and want to present a bug-free UIImagePicker, avoid UIImagePickerControllerSourceTypeSavedPhotosAlbum



回答6:

Grand central dispatch is your friend, using this method you won't see the status bar appear at all when the picker is displayed or afterwards

- (void)hideStatusBar
{
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)])
    {
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    }
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self hideStatusBar];
    double delayInSeconds = 0.2;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self hideStatusBar];
    });
}