Sunday, August 27, 2017

How to Hide Status Bar in iOS Programmatically

In this tutorial, I am going to show how to hide status bar of iPhone from you iOS project.
Normally it is discouraged to permanently hide the status bar. Hide this status bar when the users do not need to check out the status bar information (ex. battery charge, network connection, time etc.). A perfect example for this kind of situation is when you need to open a full-screen image from "Photos" the status bar is hidden.

Hiding status bar is very easy thing to do. You just one line of code to do it. Look at the code snippet given below -
-(BOOL) prefersStatusBarHidden{
   return YES;
}

If you want to hide the status bar from a viewcontroller then add this code snippet in the implementation file of that viewcontroller. Look at the video. In that video, you can see the application has two views. The first view has no status bar and the other has it. That means the second viewcontroller implementation file does not have this status bar hidden code.

Video: Hiding the status bar from a specific ViewController

The situation I have mentioned above there are two viewcontrollers are involved. But what if you have to show/hide the status bar staying on the same viewcontroller (like "Photos" app). This is a quite easy task to do.

In the button tap event, you just have to call another method and set the visibility status of the status bar. Another thing you have to do is just remove the hardcore boolean value from the prefersStatusBarHidden method and set the visibility status here.

The button tap event:
- (IBAction)ButtonTap:(id)sender {
       if( [UIApplication sharedApplication].statusBarHidden){
           /// Here visible is a instance variable
           visible = NO; /// setting the visibilty status.
       }else{
           visible = YES;
       }
     [self setNeedsStatusBarAppearanceUpdate]; /// This is the another new method need
}

And the prefersStatusBarHidden method:
- (BOOL)prefersStatusBarHidden {
    //return visible; /// removed the hradcore boolean value
    return visible; /// setting the visibility status from the button tap event
}

The result will look like Video 2.

Video 2 : Hiding the status bar from the same ViewController on button tap

Hopefully, this tutorial helped you.


No comments:

Post a Comment