2
votes

I'm working on an app where we use the white status bar tint and a dark background for the navigationBar. There is one scene where we want the navigationBar hidden but it also takes away the background color for the status bar. Is there a simple solution to keep a dark background up with hiding the navigationBar at the same time?

My code to hide the navigation bar is:

[self.navigationController setNavigationBarHidden:YES];

or in Swift:

self.navigationController?.navigationBarHidden = true
3
last sentence you mean keep a white background right?hasan
@hasan83 no, it needs to be dark because we're using the light style.user5104686
got it. can you provide the code that you used to style your navigation bar. (code that you used to set the color)hasan
do you have translucent YES or NO?hasan
It's not translucent, but I could set it to be. Wouldn't that keep the frame though? We need the tableview to be right below the status bar.user5104686

3 Answers

1
votes

Assuming you are developing for iOS7+: The statusbar doesn' have any background color on its own. In fact, the reason you are seeing a dark background when the navigation bar is visible, is because it extends upwards underneath the statusbar. So if you want to keep the status bar background you can simply add a view with an appropriate background color to the current scene (viewController, window , etc.). Give it a frame of UIApplication.sharedApplication.statusBarFrame.

-- UPDATE 1 --

Sample code (Swift 3) Gives you a solid black status bar background:

class ViewController: UIViewController {
  private let statusBarUnderlay = UIView()

  override func viewDidLoad() {
    super.viewDidLoad()

    statusBarUnderlay.backgroundColor = UIColor.black
    view.addSubview(statusBarUnderlay)
  }

  override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    statusBarUnderlay.frame = UIApplication.shared.statusBarFrame
  }
}


-- UPDATE 2 --

While we're at it. The above code is not how you should lay out your views. Instead, subclass UIView and do your layout there. Then override loadView of your UIViewController subclass and return an instance of your custom view.


class View: UIView {
  private let statusBarUnderlay = UIView()

  override init(frame: CGRect) {
    super.init(frame: frame)

    statusBarUnderlay.backgroundColor = UIColor.black
    addSubview(statusBarUnderlay)
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("Storyboards are incompatible with truth and beauty.")
  }

  override func layoutSubviews() {
    super.layoutSubviews()

    statusBarUnderlay.frame = UIApplication.shared.statusBarFrame
  }
}
1
votes

All I needed to do was add a secondary view and set it to below the status bar like this:

-(void)viewForStatusBar {
    UIView *view = [UIView new];
    [self.view addSubview:view];
    view.backgroundColor = [UIColor blackColor];        
    view.frame = CGRectMake(0, -20, [UIScreen mainScreen].bounds.size.width, 20);
}

Or in Swift:

func viewForStatusBar() {
        let view = UIView()
        self.view.addSubview(view)
        view.backgroundColor = .blackColor()
        view.frame = CGRectMake(0, -20, UIScreen.mainScreen().bounds.size.width, 20)
    }
0
votes

Here's the implementation of LTNavigationBar

I think it may help you with.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY > 0) {
        if (offsetY >= 44) {
            [self setNavigationBarTransformProgress:1];
        } else {
            [self setNavigationBarTransformProgress:(offsetY / 44)];
        }
    } else {
        [self setNavigationBarTransformProgress:0];
        self.navigationController.navigationBar.backIndicatorImage = [UIImage new];
    }
}

- (void)setNavigationBarTransformProgress:(CGFloat)progress
{
    [self.navigationController.navigationBar.transform = CGAffineTransformMakeTranslation:(0, -44 * progress)];
}

It makes the navigation bar hidden and status bar have the same background color as navigation bar when scrolling the view.If you don't need scrolling, you can just call [self setNavigationBarTransformProgress:1]