After spending hours of experiment and searching for answer; particularly this answer. With a bit tweaking, I have successfully make it, now the top gap 20px is gone between transition!
Suppose we have a BOOL isStatusBarEnabled
ivar that will indicate whether we should have status bar hidden or not, (eg: when accessing NSUserDefault
for checking boolValueForKey
).
So, we first check for whether if statusBar is already hidden or not via [[UIApplication sharedApplication] isStatusBarHidden]
, if it is not hidden (== being shown), we hide it! Else, do otherwise!
To fix 20px when status is shown - but navigation is not properly pushed down, just add 20 point to origin.y
of self.navgigationController.navigationBar.frame
.
Do the same when we want to hide status bar, just remove that 20 point to origin.y
of self.navgigationController.navigationBar.frame
so just leave it 0
.
this is it!
@implementation SomeViewController {
BOOL isStatusBarEnabled;
}
// ...
- (void)toggleStatusBar
{
UINavigationBar *navBar = self.navigationController.navigationBar;
if ([[UIApplication sharedApplication] isStatusBarHidden]) {
// Change to regular mode
// Show status bar
[[UIApplication sharedApplication] setStatusBarHidden:NO
withAnimation:UIStatusBarAnimationSlide];
[UIView animateWithDuration:0.3
animations:^{
navBar.frame = CGRectMake(navBar.frame.origin.x, 20, navBar.frame.size.width, navBar.frame.size.height);
} completion:nil];
} else if (![[UIApplication sharedApplication] isStatusBarHidden]) {
// Change to fullscreen mode
// Hide status bar
[[UIApplication sharedApplication] setStatusBarHidden:YES
withAnimation:UIStatusBarAnimationSlide];
[UIView animateWithDuration:0.4
animations:^{
navBar.frame = CGRectMake(navBar.frame.origin.x, 0, navBar.frame.size.width, navBar.frame.size.height);
} completion:nil];
}
}
// ...
... then, in my case, I have a setting key to let user choose toggle show/hide status bar.
// ...
- (void)onDefaultsChanged:(NSNotification*)aNotification
{
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
isStatusBarEnabled = [standardDefaults boolForKey:kStatusBar];
if (isStatusBarEnabled) {
if ([[UIApplication sharedApplication] isStatusBarHidden]) {
// Change to regular mode
// Show status bar
[self toggleStatusBar];
} else {
// Change to fullscreen mode
// Hide status bar
[self toggleStatusBar];
}
// ...
}
that's it!