UIViewController background image that spans up behind the navigation bar?

Viewed 1311

I have an interesting problem that I'm not sure can be solved in a standard way:

I have a UIViewController with a UIImageView positioned on the top, flush with the top space to container. Standard stuff.

What I want trying to do, however, was see if there is a way to get it so the image runs up behind the navigation bar as well (so the bar is overlayed on top of the background image).

My thought is this is not possible, because the image cannot span two view controllers. I can certainly have it on one view controller, and even have the navigation bar have a background image, but I can't make it so BOTH contain the same image cohesively, correct?

I figured the only way to achieve something like this is to hide my navigation bar, and make my own "cheat" one that actually resides inside the viewcontroller to simulate it.

Any ideas or is this breaking the fundamentals.

Edit: After trying both suggestions, it ends up doing this (in both cases): https://imgur.com/a/RwALHac

So it's almost like it sets the top bar to be one image, and the bottom tableview to be the image but zoomed out

Any ideas?

2 Answers

Make the transparent navigation bar to visible the background images. Add the following code to your navigationbar extention.

func transparentNavigationBar() {
        self.navigationController!.navigationBar.setBackgroundImage(UIImage(),for: UIBarMetrics.default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.isTranslucent = true
        self.navigationController!.view.backgroundColor = UIColor.clear
        self.navigationController?.navigationBar.backgroundColor = UIColor.clear
    }

Actually this can be quite simple. Make sure your navigation controller's root view controller has it's edges extended under top bars (simple checkbox in interface builder), and then make the navigation bar transparent with this code:

navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationBar.shadowImage = UIImage()
.navigationBar.isTranslucent = true

This will make your view controller (and thus, it's background image) to extend all the way to the top of the screen, with the navigation bar overlapping. The navigation bar itself will not be visible, but it's contents will be (buttons, title, etc.)

Related