How to hide the status bar programmatically in iOS 7?
Asked Answered
P

12

37

In , how can I hide the programmatically? I am using XCode 4.6.1 () and I want to implement this in XCode itself.

Paederast answered 28/9, 2013 at 14:14 Comment(0)
I
72

in iOS7 you should implement in your viewController

- (BOOL)prefersStatusBarHidden {
    return YES;
}
Intrados answered 28/9, 2013 at 22:5 Comment(0)
M
33

you can hide status bar to set the key value "View controller-based status bar appearance" NO in plist. This is easiest way.

or You can hide in code by using property statusBarHidden of UIApplication class.

[[UIApplication sharedApplication] setStatusBarHidden:YES];

Swift 3.0

Hide status bar for any particular view controller

override var prefersStatusBarHidden: Bool {
    get {
        return true
    }
}

Hide Status bas across the application

UIApplication.shared.isStatusBarHidden = true

and set the key value "View controller-based status bar appearance" NO in info plist of project.

Maquis answered 28/9, 2013 at 15:14 Comment(4)
This doesn't work on iOS 7. The correct answer is by @user2826529Madalynmadam
Works for me. Ensure you set the property he mentions in the plist appropriately.Norwegian
Far the best solution. Forget all the stuff above. Apple made it way too complicated.Dancer
Override code works perfectly in iOS 13, Xcode 11.3, Swift 5. UIApplication.shared.isStatusBarHidden is deprecated.Derinna
C
18
- (void)viewDidLoad
{
    [super viewDidLoad];
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)])
    {
        [self prefersStatusBarHidden];
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    }
    else
    {
        // iOS 6
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
    }
}

// Add this method
- (BOOL)prefersStatusBarHidden {
    return YES;
}
Cogitable answered 29/9, 2013 at 6:3 Comment(6)
It did not work for me. I have used this same but the status bar always shown. Please help..Paederast
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide]; This will work and there is a property in info.plist where you can hide the status bar.Cogitable
After hiding the statusbar, how can it be shown again? Because I want to alter the statusbar visible status programmatically.Giorgia
There is no need to perform the selector.Dehydrogenase
@AndrasHatvani The question specified using Xcode 4, which means that he doesn't have the iOS 7 API. He uses performSelector to avoid "no method found" warning.Plano
@Giorgia See my answer if you still need to do this.Plano
M
5

To hide for a specific ViewController (and then turn back on) when View controller-based status bar appearance set to NO:

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
}
Mirianmirielle answered 1/6, 2014 at 18:20 Comment(0)
P
3

If you need to hide/show it on a given view controller dynamically you can do something like this.

(Although I recommend just using - (BOOL)prefersStatusBarHidden to return your preference if you don't need it to change.)

// view controller header 
@interface MyViewController : UIViewController  {
    BOOL shouldHideStatusBar;
}
@end


@implementation

- (BOOL)prefersStatusBarHidden {
    return shouldHideStatusBar; // backed by your instance variable
}

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

    // Don't call this on iOS 6 or it will crash since the 
    // `setNeedsStatusBarAppearanceUpdate` method doesn't exist
    [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];

    // [self setNeedsStatusBarAppearanceUpdate]; // (if Xcode 5, use this)
}

@end
Plano answered 2/1, 2014 at 20:15 Comment(0)
C
1

In case of iOS >= 7.0 use following code :

Syntax:

// Present in UIViewController of UIKit Frameworks
- (BOOL)prefersStatusBarHidden NS_AVAILABLE_IOS(7_0); // Defaults to NO

Usage:

- (BOOL)prefersStatusBarHidden {
    return YES;
}

In iOS < 7.0 use following code :

Syntax:

// Present in UIApplication of UIKit Frameworks
- (void)setStatusBarHidden:(BOOL)hidden withAnimation:(UIStatusBarAnimation)animation NS_AVAILABLE_IOS(3_2);

Usage:

[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
Chari answered 7/2, 2014 at 12:20 Comment(0)
B
1

Swift 4.1

step1. Set View controller-based status bar appearance in your info.plist to YES

step2. Type some code in your UIViewController, status bar will hide when you present the UIViewController.

private var statusBarIsHidden = false

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    statusBarIsHidden = true
    setNeedsStatusBarAppearanceUpdate()
}
override var prefersStatusBarHidden: Bool {
    return statusBarStatus
}
Burnt answered 23/8, 2018 at 20:15 Comment(0)
R
0

Try this

[[UIApplication sharedApplication] setStatusBarHidden:YES];
Rigsby answered 7/3, 2014 at 10:54 Comment(0)
A
0

If anyone wanted the most updated way to do it (Swift 2.1 and latest Xcode 7.2)

  1. Set "View controller based status bar appearance to NO in your info.plist"

  2. UIApplication.sharedApplication().statusBarHidden = true // put inside app delegate somewhere (applicationWill or DidFinishLaunchingWithOptions:

Ashliashlie answered 18/12, 2015 at 21:15 Comment(0)
P
0

My experience is that you need both the code and the value in the info.plist file in iOS 9 / Xcode 7.3.

Add this to your viewDidLoad method.

[[UIApplication sharedApplication] setStatusBarHidden:YES];

Add this to your info.plist file as a boolean value and set it to NO:

View controller-based status bar appearance
Postprandial answered 19/8, 2016 at 16:48 Comment(0)
S
0

If using iOS 9.0+ and Swift. If you want to have status bar hidden in some view controllers but not all - make sure to have View controller-based status bar appearance value in Info.plist set to YES else same parameters will be used across all view controllers.

And override prefersStatusBarHidden in subclass of UIViewController

override var prefersStatusBarHidden: Bool {
    get {
        return true
    }
}

Similar can be done to change preferredStatusBarStyle

override var preferredStatusBarStyle: UIStatusBarStyle {
    get {
        return .lightContent
    }
}
Slippery answered 21/3, 2018 at 13:0 Comment(0)
I
0

on swift 4:

    UIApplication.shared.isStatusBarHidden = ..true/false
Intension answered 22/3, 2018 at 17:55 Comment(1)
Setter for 'isStatusBarHidden' was deprecated in iOS 9.0: Use -[UIViewController prefersStatusBarHidden]Selfdenial

© 2022 - 2024 — McMap. All rights reserved.