7

I study related iOS 7 status-Bar Maintain, but am I facing an issue while I am using UIImagePickerController. Allow editing that Editing Window shows 20 pixels space at the top bar.

I used the code and first I set UIViewControllerBasedStatusBarAppearance to NO in info.plist and set the delegate method:

Appdelegate.h

@property (retain, nonatomic) UIWindow *background;

Appdelegate.m

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {

    [application setStatusBarStyle:UIStatusBarStyleLightContent];
    self.window.clipsToBounds =YES;
    self.window.frame =  CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);

    self.window.bounds = CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height);

    background = [[UIWindow alloc] initWithFrame: CGRectMake(0, 0, self.window.frame.size.width, 20)];
    background.backgroundColor =[UIColor redColor];
    [background setHidden:NO];
}

In my home view controller that has no effect, so I put in my home viewController, one method for changing the background color of statusbar:

-(void)viewWillLayoutSubviews{

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
    self.view.clipsToBounds = YES;
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenHeight = screenRect.size.height;
    self.view.frame = CGRectMake(0, 20, self.view.frame.size.width,screenHeight-20);
    self.view.bounds = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"userIsMale"]) {
        [tempWindow setBackgroundColor:[UIColor colorWithRed:(46.0/255.0) green:(134.0/255.0) blue:(255.0/255.0) alpha:1]];
    }
    else {
        [tempWindow setBackgroundColor:[UIColor colorWithRed:(246.0/255.0) green:(26.0/255.0) blue:(113.0/255.0) alpha:1]];
    }

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

Now when I present UIImagePickerController allow editing window that shows it like this:

Enter image description here

I tried with this solution for hide statusbar while present UIImagePickerController:

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

And show status bar code in ViewWillApear.

I got this type of result:

Enter image description here

Where am I doing wrong and how do I solve this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144

5 Answers5

3

Have a try with:

-(BOOL)prefersStatusBarHidden { return YES; }

It will hide the status bar.

On iOS 7, if you want to use setStatusBarHidden:, you need to set the View controller-based status bar appearance as NO in info.plist.

I hopes it will give you some hint.


Edited:

I found the problem.

This is the log the first time you show the homeViewController at viewWillAppear:

(lldb) po [[UIApplication sharedApplication] windows]
<__NSArrayM 0x8a74560>(
<UIWindow: 0x8e33360; frame = (0 20; 320 548); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x8e348b0>; layer = <UIWindowLayer: 0x8e3c6e0>>,
<UIWindow: 0x8c1db40; frame = (0 0; 320 20); gestureRecognizers = <NSArray: 0x8c1e170>; layer = <UIWindowLayer: 0x8c1da80>>
)

And this is the log after dismissing the imagePicker and call the viewWillAppear:

(lldb) po [[UIApplication sharedApplication] windows]
<__NSArrayM 0x8c1a700>(
<UIWindow: 0x8e33360; frame = (0 0; 320 568); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x8e348b0>; layer = <UIWindowLayer: 0x8e3c6e0>>,
<UIWindow: 0x8c1db40; frame = (0 0; 320 20); gestureRecognizers = <NSArray: 0x8c1e170>; layer = <UIWindowLayer: 0x8c1da80>>,
<UITextEffectsWindow: 0x8c53380; frame = (0 0; 320 568); hidden = YES; opaque = NO; gestureRecognizers = <NSArray: 0x8c538a0>; layer = <UIWindowLayer: 0x8c53520>>
)

The default window size changed. That is the reason the status bar can't be shown.

I edited your code like this, and it works for me:

 - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    if(OVER_IOS7){
        [self.navigationController.navigationBar setTranslucent:NO];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
}

-(void)viewWillLayoutSubviews{

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        self.view.clipsToBounds = YES;
        CGRect screenRect = [[UIScreen mainScreen] bounds];
        CGFloat screenHeight = screenRect.size.height;
        self.view.frame = CGRectMake(0, 20, self.view.frame.size.width,screenHeight-20);
        self.view.bounds = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

        UIWindow *defaultWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:0];

        defaultWindow.frame =  CGRectMake(0,20,320,548);
        defaultWindow.bounds = CGRectMake(0,20,320,548);

        UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];

        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"userIsMale"]) {
            [tempWindow setBackgroundColor:[UIColor colorWithRed:(46.0/255.0) green:(134.0/255.0) blue:(255.0/255.0) alpha:1]];
        }
        else {
            [tempWindow setBackgroundColor:[UIColor colorWithRed:(246.0/255.0) green:(26.0/255.0) blue:(113.0/255.0) alpha:1]];
        }

        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    }
}

- (IBAction)showImagePicker:(id)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];

    [picker setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
    [self presentViewController:picker animated:YES completion:^{
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
    }];
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Steven Jiang
  • 1,006
  • 9
  • 21
  • one's i hiddne that ok after dismiss uiimagepicker then when i return show then it statusbar going to change – Nitin Gohel Jan 11 '14 at 07:40
  • thx for effort but with your code replace then while Home view appar there status Bar appear over navigation bar like we faced this issue normally in ios7 statusBar issue – Nitin Gohel Jan 11 '14 at 09:44
  • @NitinGohel Sorry, I don't quite understanding your meaning.In my understanding, you are making the status bar appears in home view, and disappears in image picker view.So that's my code's goal. – Steven Jiang Jan 13 '14 at 02:10
0

Try to present UIImagePickerController by:

UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
[tempWindow.rootViewController presentViewController:myImgPickerController animated:YES completion:nil];

I don't know if it will help you, but you maybe try it... :)

Another option is when you present UIImagePickerController. At that time set your view's frame as it was like at first and also hide status bar...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
iPatel
  • 46,010
  • 16
  • 115
  • 137
0

I also faced the same issue but there is only 1 way to overcome it.

Go to the info.plist file and set the value for "View controller-based status bar appearance" to "NO".

Anubrata Santra
  • 666
  • 5
  • 11
0

I got a similar issue, but when I come back from the camera roll to my UIViewController. I solved the problem with the follow code in the controller that pushs the UIImagePickerController:

First I open the Picker

- (void)showPhotoPicker:(UIImagePickerControllerSourceType)sourceType
{
    [self.picker setSourceType:sourceType];
    [self presentViewController:self.picker animated:YES completion:nil];
}

I keep the status bar without any problem, I saw some people have issues with that, I didn't

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

It is really important to call view needs layout on completition

// Apply photo selected
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self.enterPicture setImage:info[UIImagePickerControllerEditedImage]];
    [picker dismissViewControllerAnimated:YES completion:^(void){[self.view setNeedsLayout];}];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:^(void){[self.view  setNeedsLayout];}];
}

And here is the magic:

// fix the status bar & navigation bar after close the photo album on IOS7
-(void)viewWillLayoutSubviews
{
    UINavigationBar *navBar= [[self navigationController] navigationBar];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7 && navBar) {
        CGFloat statusBarHeight = [[UIApplication sharedApplication] isStatusBarHidden] ? 0.0f : 20.0f;
        [navBar setFrame:CGRectMake(0, statusBarHeight, navBar.frame.size.width, navBar.frame.size.height)];
        [self.view setBounds:CGRectMake(0, (statusBarHeight + navBar.frame.size.height) * -1, self.view.bounds.size.width, self.view.bounds.size.height)];
    }
}
Adriano Spadoni
  • 4,540
  • 1
  • 25
  • 25
0

Just add to controller:

- (void)viewWillAppear:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
}
George
  • 756
  • 1
  • 9
  • 16