开发者

iOS SDK - How to get the status bar back when using UIImagePickerController?

开发者 https://www.devze.com 2023-01-12 09:51 出处:网络
As soon as I add a UIImagePickerController sub view to my view the status bar disappears and I can\'t get it back. Is there any way to keep the status bar visible?

As soon as I add a UIImagePickerController sub view to my view the status bar disappears and I can't get it back. Is there any way to keep the status bar visible?

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;


[self.view addSubview:imagePicker.view];

[imagePicker viewWillAppear:YES];
[im开发者_Go百科agePicker viewDidAppear:YES];

[[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO];


I had to do the same thing in a camera app as well. Apparently, in addition to setting the status bar to not be hidden, you also have to reset its style after the camera view makes it disappear. Try this:

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


The accepted answer's solution got deprecated meanwhile.

Use

[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

instead of

[[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO];

Valid values for the animation parameter are UIStatusBarAnimationNone, UIStatusBarAnimationFade, UIStatusBarAnimationSlide. Details are found in the documentation.


After reading this and finding none of the answers worked, I managed to get it working by doing the following:

• Setting a delegate for the UIImagePickerController
• In that delegate, 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:NO];
}


Add your UIImagePicker to the root view (i.e. a Navigation Controller or TabbarController)

[self.tabBarController presentModalViewController:imagePickerController animated:YES];

After that you can use

- (void)imagePickerController:(UIImagePickerController *)picker 
            didFinishPickingImage:(UIImage *)image
                      editingInfo:(NSDictionary *)editingInfo
{
      // do your stuff
     [picker dismissModalViewControllerAnimated:YES];
}

to close your ImagePicker.


well, I know you are not supposed to do this, but if you subclass UIImagePickerController, you can put that in your custom class:

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];
}


None of the solutions worked on iOS 5.1.1 Tim's solution worked on iOS 4.2.1 The only way I was able to fix the problem on iOS 5.1.1 was like that

-(void)viewDidAppear:(BOOL)animated
{
    double delayInSeconds = 0.01;
    dispatch_time_t popTime = 
            dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [[UIApplicationsharedApplication] setStatusBarHidden:NO];
});

which is very hacky and wrong.

I spent half a day looking for a solution and then decided to just use AVFoundation approach and it took me an hour to implement the same basic photo capture that I needed using AVCaptureSession and AVCaptureStillImageOutput. And it works better too - AVCaptureSession starts faster than UIImagePickerController and AVCaptureVideoPreviewLayer has a much better frame rate on modern devices compared to UIImagePicker camera preview.

0

精彩评论

暂无评论...
验证码 换一张
取 消