简体   繁体   中英

NavBar overlaps Status Bar in iOS 13 Swift

In some devices as iPhone 7 and 8 the navigation bar overlaps status bar after do navigating, the parent view controller has status bar hidden but I show and hide it in viewWillAppear and viewWillDisappear respectively. I tested in iOS 12 and it works.

I am using prefersStatusBarHidden to hidden the status bar.

Here the Image Navbar Overlaps Status bar

Update: Here an example project: https://github.com/FranklinSamboni/NavBarTestSwfit . It work fine in iOS 12 but with iOS 13 the navigation bar overlaps status bar in iPhone 7,8

Images with iPhone 8 (simulator) Home The second view Second View

Okay, I'm going to give one of those wishy-washy answers.

  • On the one hand, you've definitely found a new behavior in iOS 13. When you hide the status bar, the navigation bar shrinks. You could term this a bug in iOS 13...

  • On the other hand, it could be argued that what you are doing is wrong. If you have a navigation bar, you already cannot hide the status bar on a device without a bezel (iPhone X etc.), and now Apple seems to be assuming that if you have a navigation bar you won't hide the status bar at all . And that is a reasonable assumption, because it makes no sense to hide the status bar in portrait when there is a navigation bar, and especially in some children of the navigation controller but not others.

So you could file a bug report on this, but I don't think you'll get any joy from it. Apple is likely to reply that this is intended, or is at least a consequence of doing something they don't want to support. You have a navigation bar; allow the status bar to show.

I faced the same problem, after a few hours research, I found a not perfect but worked solution. Hope it will work for you.The code was written with Objective-C.

In viewDidAppear method of the secondViewController, hide statusBar first and show it at once.

  1. Declare a member variable BOOL statusBarHidden in secondViewController
  2. Implement prefersStatusBarHidden method from UIViewController
     - (BOOL)prefersStatusBarHidden { return statusBarHidden; }
  3. Create a new method setStatusBarHidden
     - (void)setStatusBarHidden:(BOOL)hidden { if (statusBarHidden;= hidden) { statusBarHidden = hidden; [self setNeedsStatusBarAppearanceUpdate]; } }
  4. Call setStatusBarHidden in viewDidAppear
     - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self setStatusBarHidden:YES]; [self setStatusBarHidden:NO]; }

You were always doing this wrong. Use two view controller classes. Get rid of your navigation controller override. This is all it takes:

class ViewController: UIViewController {

    override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation{
        return .fade
    }

    override var prefersStatusBarHidden: Bool { return true }
}

class ViewController2: UIViewController {

    override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation{
        return .fade
    }

    override var prefersStatusBarHidden: Bool { return false }

}

Or whatever your preference is. In other words, just let each view controller dictate its own status bar preference, and the navigation controller obeys automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM