0

I want to add capability for the user to change the System Status bar style from Light to Dark.

On app launch the style is Light.

I have added the following code on button click:

//on button click
[self setNeedsStatusBarAppearanceUpdate];

- (UIStatusBarStyle)preferredStatusBarStyle
{
    if(darkMode) {
        return UIStatusBarStyleDefault;
    } else {
        return UIStatusBarStyleLightContent;
    }
}

But the prefferredStatusBarStyle method is never called and no change to the status bar occurs.

s.d.
  • 343
  • 3
  • 12
  • Review http://stackoverflow.com/a/17768797/1226963 – rmaddy Dec 21 '15 at 02:55
  • i have seen that. Does not seem to apply in my case where i want to change the status bar on button click – s.d. Dec 21 '15 at 03:06
  • my bad.... i had the following set to NO in the `info.plist`. issue fixed `View controller-based status bar appearance` – s.d. Dec 21 '15 at 03:15

2 Answers2

1
#import "ViewController.h"

@interface ViewController ()
{
    int click;
}   
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
} 
- (IBAction)ButtonClick:(UIButton *)sender
{
        sender.selected=!sender.selected;
        if (sender.isSelected)
        {
            click=1;
        }
        else
        {
            click=0;
        }
        [self setNeedsStatusBarAppearanceUpdate];
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
       if(click)
        {
            return UIStatusBarStyleDefault;
        } else
        {
            return UIStatusBarStyleLightContent;
        }
 }
}
@end
Gaurav Gudaliya
  • 111
  • 1
  • 1
  • 9
1

Swift 3

Set that the status bar needs to be updated when you click on the button. In this way 'preferredStatusBarStyle' will be called.

setNeedsStatusBarAppearanceUpdate()
Andrea.Ferrando
  • 928
  • 11
  • 21