3

I am using UIScrollView on iOS 9 with two pages and for the first page I want the statusbar text to be black and the second page white. So when I slide to the second page I want the statusbar color to turn white and black if I slide to the first page.

I have tried setting the UIViewControllerBasedStatusBarAppearance to false in the plist file and then programmatically calling:

UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.BlackTranslucent;
SetNeedsStatusBarAppearanceUpdate();

But without luck, the statusbar text appears white and it stays white even when calling UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.BlackTranslucent;

Any idea how to manually change the statusbar text color within the same view controller?

doorman
  • 15,707
  • 22
  • 80
  • 145

3 Answers3

5

If you're using a Navigation Controller for your view controllers, try changing the property:

NavigationController.NavigationBar.BarStyle

"Default" should be black and "LightContent" should be white. I believe the Black options are deprecated.

Otherwise, you could opt for View Controller based Status Bar Style in your Info.plist and use the preferredStatusBarStyle property as mentioned here.

In your ViewDidLoad method, do SetNeedsStatusBarAppearanceUpdate();

Add the following method:

 public override UIStatusBarStyle PreferredStatusBarStyle()
 {
        return UIStatusBarStyle.LightContent; //Or Default/Black/etc.
 }

Note: Using UIApplication.SharedApplication to change the status bar style has been deprecated in iOS 9. Use the above methods instead. Hope this helped. Cheers!

Community
  • 1
  • 1
Derpity Derp
  • 120
  • 7
  • This worked, I had to use UIStatusBarStyle.Default instead of BlackTranslucent for the black color. But animation is not working for some reason. I set the UIStatusBarAnimation PreferredStatusBarUpdateAnimation to UIStatusBarAnimation.Fade but it's not working. – doorman Jul 11 '16 at 18:07
  • I've updated the question with the "Default" == black thing you mentioned. :) I'll look into the animation problem and report back if I find anything. – Derpity Derp Jul 11 '16 at 18:16
  • 1
    Great, thanks. Regarding the animation part, I had to wrap it update command into the command animation like this:UIView.Animate(0.3f, 0, UIViewAnimationOptions.CurveEaseOut, SetNeedsStatusBarAppearanceUpdate, () => {}); – doorman Jul 11 '16 at 19:29
4

Go to your application Plist and add this as new row & set it as NO.

View controller-based status bar appearance NO

Add a bool to determine state of UIStatusBar colour & add a Toggle method

@property(nonatomic) BOOL black;


-(void)toggleStatuSBar:(id)sender{

    if(black) {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
        black = NO;

    }else {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
        black = YES;
    }
}
Rivera
  • 10,792
  • 3
  • 58
  • 102
Nattudurai
  • 856
  • 7
  • 9
0

If use Navigation controller then add:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
}