senty
senty

Reputation: 12857

Adding Navigation Bar on Tab-Bar Based App

I have a tab-bar based app and I want to add Navigation Bar at the top of the app. Please note that I am using a library called PageMenu that creates 2 TableViews inside 1 parent ViewController.

What I tried was, adding a new ViewController and Editor->Embed in Navigation Bar. Place it before Tab Bar Controller, ctrl+drag to Tab Bar Controller to set the relationship of root view. Finally set Nav Bar Controller as initial view controller. But this fails like this:

enter image description here

(Top became pretty weird, blurry and the sub-header of PageMenu got disappeared. Maybe it's under that blurry thing because I can still swipe between 2 table views.

enter image description here

Secondly, I tried removing the Navigation Controller, and add Navigation Bar to the ViewControllers manually. This worked for table view and view controllers but not the PageMenu one. When I tried it on PageMenu Controller, it didn't show any navigation bar.

Please note that, in the Demo, they used Navigation Bar as Parent and sub-TableViews, and they achieved Navigation Bar with this as well as Storyboard > Navigation Controller:

override func viewDidLoad() {
    super.viewDidLoad()

    self.title = "HEADER"
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
    self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
  }

Lastly, I tried..

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

    let nav1 = UINavigationController()
    let first = ViewController(nibName: nil, bundle: nil)
    nav1.viewControllers = [first]

    let second = SecondViewController(nibName: "SecondViewController", bundle: nil)
    let nav2 = UINavigationController()
    nav2.viewControllers = [second]

    let tabs = UITabBarController()
    tabs.viewControllers = [nav1, nav2]

    self.window!.rootViewController = tabs;
    self.window?.makeKeyAndVisible();

    return true
}

But the result I get is:

enter image description here

What I want to achieve (but with TabBarController; NavBar just for header):

enter image description here

What I have now is this. I just want to add NavigationBar at the top of it like the above PageMenu example

enter image description here


Update: Lasly, I also tried: enter image description here

But same issue: enter image description here

Upvotes: 4

Views: 6898

Answers (2)

Nimit Parekh
Nimit Parekh

Reputation: 16864

You can also create like wise story board that helps to solve your problem.

enter image description here

Here I can created sample code what you want no single line code change but changes into storyboard only.

Download source code from here.

Upvotes: 7

Rushang Prajapati
Rushang Prajapati

Reputation: 596

Check this image

Not add Editor->Embed in Navigation Bar before Tab Bar Controller add Editor->Embed in Navigation Bar before of View controller which you connect from Tab Bar controller.

Because its work for particular Tab Vise so we have to add Editor->Embed in Navigation Bar before of View controller

Upvotes: 3

Related Questions