0

How to add view above navigation bar? I have a custom navigation controller and I want to present a view above nav bar (like on the screen), so it should be visible on other ViewControllers

Would be great if the solution will be on storyboard. Like that

Tried to add on UIWindow did't help.

Artyom
  • 371
  • 1
  • 4
  • 14
  • Please can you edit your question for clarity: "I have a custom navigation controller and _I want to a view above everything_ (like on the screen), so it should be visible on other ViewControllers" isn't that clear. Are you saying you want to present things on(?) the statusbar, or on the nav bar, or both? – 10623169 Apr 24 '19 at 11:05
  • I want to present a view above nav bar it doesnt matter if it will cover status bar – Artyom Apr 24 '19 at 11:09
  • put a uiview on it and use gradient to get the desired solution – Wings Apr 24 '19 at 11:14
  • self.navigationController.navBar.addSubView(requiredViewToAdd) something like this in your initialViewController – Abu Ul Hassan Apr 24 '19 at 11:16
  • Possible duplicate of [ios 11 custom navbar goes under status bar](https://stackoverflow.com/questions/46326554/ios-11-custom-navbar-goes-under-status-bar) – dahiya_boy Apr 24 '19 at 11:20

1 Answers1

1

Swift 4.2, Xcode 10+

Okay, from what I can tell (via your comment reply, though it still isn't 100% clear), the best solution to your question would be to make the navigation bar transparent, such that you can see any navigationController-presented view controllers underneath it. For this, I'd suggest the following extension to UIViewController:

extension UIViewController {    
    func setupTransparentNavigationBarWithBlackText() {
        setupTransparentNavigationBar()
        //Status bar text and back(item) tint to black
        self.navigationController?.navigationBar.barStyle = .default
        self.navigationController?.navigationBar.tintColor = .black
    }

    func setupTransparentNavigationBarWithWhiteText() {
        setupTransparentNavigationBar()
        //Status bar text and back(item) tint to white
        self.navigationController?.navigationBar.barStyle = .blackTranslucent
        self.navigationController?.navigationBar.tintColor = .white
    }

    func setupTransparentNavigationBar() {
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.backgroundColor = .clear
        self.navigationController?.navigationBar.isTranslucent = true
    }
}

Using either of the first two methods in viewWillAppear of your UIViewController subclasses will let you make the navigation bar completely transparent with the statusBar text + wifi/battery indicators black or white as desired. From this, you can then display anything under the navigation bar by pinning your constraints to view.bounds.topAnchor. E.g. for a transparent navigation controller with white statusBar text:

class YourViewController: UIViewController {
    override func viewWillAppear(_ animated: Bool) {
        setupTransparentNavigationBarWithWhiteText()
    }
}
10623169
  • 984
  • 1
  • 12
  • 22