How to hide status bar in UIImagepickercontroller? - ios

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?

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.

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

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

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!

If you want to disable the status bar from plist, try this:
Status bar is initially hidden : YES
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];
.
.
.
.
}

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.

Try this
[[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];
Also check this discussion.

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];
}

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

-(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.

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

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.

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];
}

Related

iOS 8: status bar doesn't work with UIImagePickerController

I've a view with UIImagePickerController..
In the whole application I have:
/* white color */
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
But in this case I need to view status bar black, so I set it:
/* black color */
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
I've do this in delegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
But seems ignoring instruction.
Works only for iOS < 8. I've already read this question and other, but I can't make it work.
Just change it from your xib / storyboad if you want to change for particular view means.
Try like this while presenting UIImagePickerViewController:
[self presentViewController:imagePickerController animated:YES completion:^{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}];
I resolved my problem in this way:
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
if(IS_IOS8_AND_UP) {
imagePickerController.modalPresentationStyle = UIModalPresentationFullScreen;
} else {
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
}
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];
And then in delegate I set the default status bar style.
Hope this help!

SKStoreProductViewController shows the status bar in iOS7

In iOS7, when a SKStoreProductViewController is presented, it shows the status bar, making it difficult to hit the [Cancel] button.
Can it be disabled?
In my app, UIViewControllerBasedStatusBarAppearance ("View controller-based status bar appearance") is set to YES.
Putting the following code anywhere in the project:
#interface SKStoreProductViewController (StatusBarFixing)
#end
#implementation SKStoreProductViewController (StatusBarFixing)
-(BOOL) prefersStatusBarHidden { return YES; }
#end
... seems to do the trick, even if it is a bit sketchy. However, it would only work if UIViewControllerBasedStatusBarAppearance is set to YES
It seems to me that the status bar is not meant to be shown here. In my opinion, it's best to just hide the status bar and everything will look like it's supposed to.
-(void)showAppInAppstore {
SKStoreProductViewController *spvc = [[SKStoreProductViewController alloc] init];
spvc.delegate = self;
// ... start activity indicator here if you wish
__weak typeof(self) weakSelf = self;
[spvc loadProductWithParameters:#{SKStoreProductParameterITunesItemIdentifier : #(APP_STOREID)}
completionBlock:^(BOOL result, NSError *error) {
// there is no way to stop this task so...
// make sure that user hasn't navigated away from "rate" screen
if(weakSelf != nil && weakSelf.isViewLoaded && weakSelf.view.window != nil) {
// ... stop activity indicator here
if(result == NO || error != nil) {
// handle error if needed
} else {
// Hide status bar
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
// Present store controller
UIViewController *controller = [UIApplication sharedApplication].keyWindow.rootViewController;
[controller presentViewController:spvc animated:YES completion:nil];
}
}
}
];
}
Now make sure that the status bar becomes visible once user is done
-(void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
[viewController dismissViewControllerAnimated:YES completion:nil];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
}
You can subclass SKStoreProductViewController and control status bar appearance from that point. That's simple and elegant.
SKStoreProductViewControllerWithoutStatusBar.h
#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>
#interface SKStoreProductViewControllerWithoutStatusBar : SKStoreProductViewController
#end
SKStoreProductViewControllerWithoutStatusBar.m
import "SKStoreProductViewControllerWithoutStatusBar.h"
#interface SKStoreProductViewControllerWithoutStatusBar ()
#property (nonatomic) BOOL wasStatusBarHidden;
#end
#implementation SKStoreProductViewControllerWithoutStatusBar
// Works if UIViewControllerBasedStatusBarAppearance == NO
- (void)viewWillAppear:(BOOL)animated {
self.wasStatusBarHidden = [[UIApplication sharedApplication] isStatusBarHidden];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[[UIApplication sharedApplication] setStatusBarHidden:self.wasStatusBarHidden withAnimation:UIStatusBarAnimationSlide];
[super viewWillDisappear:animated];
}
// Works if UIViewControllerBasedStatusBarAppearance == YES
- (BOOL)prefersStatusBarHidden {
return YES;
}
#end

How do I hide the status bar of a pushed view controller in objective c?

I have a signup form that pops up when a button is tapped.
My aim is to hide the status bar when this modal is popped up.
Here is my code:
- (IBAction)tappedJoinButton:(id)sender {
if (![PFUser currentUser]) {
PFSignUpViewController *signUpViewController = [[PFSignUpViewController alloc] init];
[signUpViewController setDelegate:self]; // Set ourselves as the delegate
// Present the sign up view controller
[self presentViewController:signUpViewController animated:YES completion:NULL];
}
}
I have set View controller-based status bar appearance to yes in my plist file. Now I'd like to choose where I hide the status bar. In this situation I'd like to hide it in the signUpViewController that pops up.
I haven't seen any answers on here showing how to hide it in a pushed view controller.
How do I achieve this?
Kind regards
If you want to hide status bar for only one ViewController the do this:
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
- (void)viewWillDisappear:(BOOL)animated{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[super viewWillDisappear:animated];
}
For your case it will be in PFSignUpViewController.
Hope this helps .. :)
Try this code
in viewDidload of PFSignUpViewController
if ([self respondsToSelector:#selector(setNeedsStatusBarAppearanceUpdate)]) {
// iOS 7
[self performSelector:#selector(setNeedsStatusBarAppearanceUpdate)];
} else {
// iOS 6
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
}
paste this function in controller
- (BOOL)prefersStatusBarHidden {
return YES;
}
you lust like ....
if ([UIApplication sharedApplication].statusBarHidden != hideStatusBar)
{
[[UIApplication sharedApplication] setStatusBarHidden:hideStatusBar withAnimation:UIStatusBarAnimationSlide];
}
Write this in your viewWillAppear...
[[UIApplication sharedApplication] setStatusBarHidden:YES];
Or try This method ....
-(void)navigationController:(UINavigationController *)
navigationController willShowViewController:(UIViewController *)
viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
Add this "View controller-based status bar" appearance in the plist and set NO
[[UIApplication sharedApplication] setStatusBarHidden:YES];

Any way to present navigation bar for UIImagePickerController, source type UIImagePickerControllerSourceTypeCamera, in iOS 7?

In iOS 6, I was using the following code to push a UIImagePickerController, of source type UIImagePickerControllerSourceTypeCamera, and to show its navigation bar. I wanted to show the navigation bar because after taking the image, I'm pushing another VC that allows the user to set some attributes in the database.
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
cameraController = [[UIImagePickerController alloc] init];
cameraController.delegate = self;
cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:cameraController animated:YES completion:NULL];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
cameraController.topViewController.title = #"Add";
cameraController.navigationBar.translucent = NO;
cameraController.navigationBar.barStyle = UIBarStyleDefault;
[cameraController setNavigationBarHidden:NO animated:NO];
}
In iOS 7 this code no longer shows the navigation bar. Does anyone know if there's a way to to get the navigation bar back for UIImagePickerController, of source type UIImagePickerControllerSourceTypeCamera?
Guess what? When imagePicker presents, it's automatic set to hidden....
All you need to do is setHidden:NO in next runloop. Like:
[self presentModalViewController:imagePicker animated:YES];
[self performSelector:#selector(showNavigationBar:) withObject:imagePicker afterDelay:0];
- (void)showNavigationBar:(UIImagePickerController*)imagePicker {
[imagePicker setNavigationBarHidden:NO];
}
#LeverkusenFan's solution works well. But instead of using a hack such as a run loop, you use the completion handler of presentViewController to achieve that effect.
[self presentViewController:cameraController animated:YES completion:^{
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
cameraController.topViewController.title = #"Add";
cameraController.navigationBar.translucent = NO;
cameraController.navigationBar.barStyle = UIBarStyleDefault;
[cameraController setNavigationBarHidden:NO animated:NO];
}];
In fact a better solution that avoids the weird animation when the navigation bar shows up and which works well when you press the back button on the nav bar is as follows:
In the delegate for the UIImagePickerController implement the following function.
- (void) navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (navigationController == self.cameraController && navigationController.viewControllers.count == 1) {
// When showing the ImagePicker update the status bar and nav bar properties.
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
navigationController.topViewController.title = self.cameraTitle;
navigationController.navigationBar.translucent = NO;
navigationController.navigationBar.barStyle = UIBarStyleDefault;
[navigationController setNavigationBarHidden:NO animated:animated];
}
}
This function will get called when the ImagePicker is shown and we only make the changes for the rootViewController of the ImagePicker (i.e. the camera screen).

lock UIImagePickerController in Portrait mode in ios app

In my IOS app, when I open the camera I have placed an image over the camera view. It looks good in portrait mode. But when it is changed to landscape mode it looks some odd. So I want to lock the UIImagePickerController in Portrait mode.
Following is my code for ui image picker controller
UIImagePickerController *imgPkr = [[UIImagePickerController alloc] init];
imgPkr.delegate = self;
imgPkr.sourceType = UIImagePickerControllerSourceTypeCamera;
How it can be locked in portrait mode....
Or, you can subclass the UIImagePickerController:
Create a new UIImagePickerController class and just add these lines to it.
- (BOOL)shouldAutorotate{
return NO;
}
Import it to the class that uses the camera and instead of using default UIImagePickerController, use the class that you created.
Your camera itself should stop from auto rotating.
This is not the best solution, but it works:
AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview:cameraOverlay];
imgPicker.cameraOverlayView = appDelegate.window.superview;
The camera on the background still rotates, but your overlay view doesn´t.
Hope it works for you
The only solution that worked for me was the category, but I had to add another method too:
#import "UIImagePickerController+NoRotate.h"
#implementation UIImagePickerController (NoRotate)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
#end
Cheers!
You don't have to "lock the UIImagePicker Controller in Portrait mode".
As you said "when it is changed to landscape mode it looks some odd"
Actually I don't know why you say it look odd.
But, here is my experience of UIImagePicker view look odd in landscape mode.
That is:
When AViewController as the root view controller.
And BViewController's view add subview to AViewController's view.
And presentModalViewController:UIImagePickerController in BViewController.
The UIImagePicker view will look odd in landscape mode.
The solution to this problem is set the UIImagePickerController as the root view controller before presentModelViewController.
The source code below show the detail:
- (void) onCameraButtonTapped:(UIBarButtonItem *)buttonItem
{
//backupRootController it's use as a backup, it will recover after close the image picker controller.
self.backupRootController = [[UIApplication sharedApplication] keyWindow].rootViewController;
UIImagePickerController * imageController = [[UIImagePickerController alloc] init];
imageController.sourceType = UIImagePickerControllerSourceTypeCamera;
imageController.delegate = self;
....
[[[UIApplication sharedApplication] keyWindow] setRootViewController:imageController];
[self presentModalViewController:imageController animated:YES];
[imageController release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[[[UIApplication sharedApplication] keyWindow] setRootViewController:self.backupRootController];
....
}
I hope this solution can help other person in the future.
--Goman
Add a category on UIImagePickerController and override it's shouldAutoRotateToInterfaceOrientation method, as follows:
#implementation UIImagePickerController (NoRotate)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Just write this code in your view controller
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(BOOL)shouldAutorotate {
return NO;}
Try this in your view controller. This worked for me.
Note: This is for ios6.0 and above
there is no need to create a subclass, just create a category for uiimagepickercontroller and put this line on it
- (BOOL)shouldAutorotate{
return NO;
}

Resources