0

I'm trying to use an UISwitch to toggle the status bar (Xcode 5, iOS 7). I currently have this code

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

This hides the status bar completely. but I want to both hide and show the status bar with an UISwitch or with a button click. How can I do it ?

I think returning a boolean value (YES or NO) to prefrersStatusBarHidden will do the job. But I couldn't find any guide about returning values to BOOL.

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

2

Have your prefersStatusBarHidden return a property of your object, such as self.hideStatusBar, then in your method for the button/switch do:

// Set self.hideStatusBar value based on what the user did
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)])
{
    // iOS 7+
    [self prefersStatusBarHidden];
    [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
}
else
{
    // iOS 6
    [[UIApplication sharedApplication] setStatusBarHidden:self.hideStatusBar withAnimation:UIStatusBarAnimationSlide];
}

The above code is from another StackOverflow answer

Make sure to give self.hideStatusBar a default value!

Community
  • 1
  • 1
Joseph Duffy
  • 4,566
  • 9
  • 38
  • 68
  • Thanks..However it didn't work. but I went through the link you gave and found the method as another answer ! – Kola Lolla Aug 16 '14 at 15:20
0

Solution by OP.

in .h file header

@interface ViewController ()
@end

BOOL shouldHideStatusBar;

code

- (BOOL)prefersStatusBarHidden {
    return shouldHideStatusBar;
}

- (void)setPrefersStatusBarHidden:(BOOL)hidden {
    shouldHideStatusBar = hidden;

    //[self setNeedsStatusBarAppearanceUpdate];
    [UIView animateWithDuration:0.33 animations:^{ //this animates the event
        [self setNeedsStatusBarAppearanceUpdate];
    }];
}

call it (in my case using an UISwitch)

[self setPrefersStatusBarHidden:NO];
Cœur
  • 37,241
  • 25
  • 195
  • 267