Agung
Agung

Reputation: 13803

How to overlap navigation bar by adding view in swift?

I want to make a custom side bar by adding a new view to the view controller, the side bar will be in the yellow color background. I want my side bar also to overlap the navigation bar/item (green background color) in my view controller. but the navigation bar/item seems can't be overlapped by my side bar view, it seems only overlap the main view.

I tried to find the answer in stackoverflow, I find this Overlap navigation bar on ios 6 with other view, but the answer is on the Objective-C, I can't read Objective-C :(

What should I do to overlap navigation bar/item ? here is the screenshot of my view controller

enter image description here

I embed the navigation controller like this enter image description here

Upvotes: 1

Views: 3070

Answers (2)

totocaster
totocaster

Reputation: 6363

You can do this by changing your UIViewController hierarchy. For this you'll need three view controllers. First will contain everything, let's call it MasterViewController; second—your main content with navigation bar; and third—drawer.

ViewController hierarchy diagram

In MasterViewController instantiate child view controllers and add them to your view controller hierarchy in viewDidLoad().

final class MasterViewController: UIViewController {
    override func viewDidLoad() {

        let drawerViewController = DrawerViewController()
        let mainViewController = MainContentViewController()
        let navigationController = UINavigationController(rootViewController: mainViewController)

        addChildViewController(drawerViewController)
        addChildViewController(navigationController)

        view.addSubview(navigationController.view)
        view.addSubview(drawerViewController.view)
    }
}

Now you have navigationController.view that you can place or animate anywhere within view.

Upvotes: 0

Mitchell Currie
Mitchell Currie

Reputation: 2809

There are plenty of implementations of slide-over or drawer containers.

What you need to do to get above the navigation bar is CONTAIN the navigation controller inside another view controller.

The stack would look like this.

  • MasterViewController
    • UINavigationController
      • RootViewController
    • Menu

See this one here:

Swift version of MMDrawerController

Upvotes: 1

Related Questions