Hiding the status bar after viewing loads the leaves with a black stripe

I had some strange problem hiding the status bar after which the view loaded. If I add the following method to the ViewDidLoad method, the status bar is completely removed from the view:

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide]; 

However, if I call this method in IBAction or another method, the status bar still bounces, but leaves a black bar of the same height as itself.

I was thinking about moving the entire view to 20 pixels, but is this really a fix? I do not want to just overlap the black bar if the status bar height changes in future OS updates.

Status bar leaving black bar

+4
source share
5 answers

Hard coding of any number always contradicts future verification. Your problems are true. There is some trick for handling statusBar hides correctly. But all the necessary information is available.

For example, a singleton UIApplication has a property called statusBarFrame , which is just like the CGRect frame. The best part is that after you setStatusBarHidden:withAnimation: this property will provide you with a new frame, even before the animation finishes. Therefore, in fact, you just stay with the basic math to customize the view frame .

In short, your gut feeling is right; always figure things out live.

I had good success with this category method. (Even when switching the call status bar in the simulator (Command-T)):

 @implementation UIApplication (nj_SmartStatusBar) // Always designate your custom methods by prefix. -(void)nj_setStatusBarHidden:(BOOL)hidden withAnimation:(UIStatusBarAnimation)animation{ UIWindow *window = [self.windows objectAtIndex:0]; UIViewController *rootViewController = window.rootViewController; UIView *view = rootViewController.view; // slight optimization to avoid unnecassary calls. BOOL isHiddenNow = self.statusBarHidden; if (hidden == isHiddenNow) return; // Hide/Unhide the status bar [self setStatusBarHidden:hidden withAnimation:animation]; // Get statusBar frame CGRect statusBarFrame = self.statusBarFrame; // Establish a baseline frame. CGRect newViewFrame = window.bounds; // Check if statusBar frame is worth dodging. if (!CGRectEqualToRect(statusBarFrame, CGRectZero)){ UIInterfaceOrientation currentOrientation = rootViewController.interfaceOrientation; if (UIInterfaceOrientationIsPortrait(currentOrientation)){ // If portrait we need to shrink height newViewFrame.size.height -= statusBarFrame.size.height; if (currentOrientation == UIInterfaceOrientationPortrait){ // If not upside-down move down the origin. newViewFrame.origin.y += statusBarFrame.size.height; } } else { // Is landscape / Slightly trickier. // For portrait we shink width (for status bar on side of window) newViewFrame.size.width -= statusBarFrame.size.width; if (currentOrientation == UIInterfaceOrientationLandscapeLeft){ // If the status bar is on the left side of the window we move the origin over. newViewFrame.origin.x += statusBarFrame.size.width; } } } // Animate... Play with duration later... [UIView animateWithDuration:0.35 animations:^{ view.frame = newViewFrame; }]; } @end 
+3
source

Why are you calling this in viewDidLoad ?

Try it in loadView?

 - (void)loadView { [super loadView]; [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide]; } 
0
source

Yes, moving a 20px view should fix your problem. Black is the lack of anything to display, not the actual black bar.

As for potential changes in state height, this fix will not work if this happens because the view will move along the height of the new status bar. If this happens, you will either have to add different offsets for different status bars, or find a completely new solution.

0
source

In viewWillAppear :

 [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade]; 

In viewDidAppear you can insert:

 self.view.window.rootViewController.view.frame = [UIScreen mainScreen].applicationFrame; 

In viewWillDisappear :

 [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade]; 
0
source

I was able to fix this problem by calling:

 [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide]; 

as recommended by others, before introducing a view controller that displays a black bar.

For example, if I had the action represented by the ViewController , I would call it that:

 - (IBAction)presentViewController:(id)sender { [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide]; ViewController *vc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; [self presentViewController:vc animated:YES completion:nil]; } 
0
source

Source: https://habr.com/ru/post/1440167/


All Articles