Search code examples
iosuinavigationcontrolleruikitstatusbar

UIKIt - Remove status bar but leave space


I know how to remove the status bar but this automatically snaps my navigation controller and its navigation bar to the top of the screen.

How can I remove the status bar but keep the 20 px space at the top of the screen, so I can put my own custom view or window in that space?


Solution

  • Create a custom UINavigationController and override the viewDidAppear

        - (void)viewDidAppear:(BOOL)animated
        {
            [super viewDidAppear:animated];
            CGRect f = self.view.frame;
            self.view.frame = CGRectMake(0,20,f.size.width,f.size.height-20);
            //the custom view for replacing the status bar.
            //you can add any custom subview you want 
            UIView *iv = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,20)];
            //assign a color with alpha less than 1.0 to make it translucent
            iv.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
            //remember to remove the iv when you don't need the navigation controller any more.
            [self.view.window insertSubview:iv aboveSubview:self.view];
        }