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
source share