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:
(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.
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:
What I want to achieve (but with TabBarController; NavBar just for header):
What I have now is this. I just want to add NavigationBar at the top of it like the above PageMenu example
Update:
Lasly, I also tried:
But same issue:
You can also create like wise story board that helps to solve your problem.
Here I can created sample code what you want no single line code change but changes into storyboard only.
Download source code from here.
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
Related
I'm trying to create UITabBarController programmatically, adding multiple NavigationControllers to it. When UITabBarController contains one NavigationController - everything works as expected (see image)
But when i add multiple NavigationControllers to UITabBarController each screen becomes black (see another image )
The same black screen is shown when switching between tabs 1, 2, 3, 4 and 5.
Here's the code how UITabBarController is created
class TabBarViewController : UITabBarController{
override func viewDidLoad() {
super.viewDidLoad()
let controllers = [HistoryViewController.self, StatsViewController.self, DashboardViewController.self, ExpenseManagerViewController.self, ProfileViewController.self]
var navControllers: [UINavigationController] = []
controllers.forEach{ ctrl in
navControllers.append(getController(from: ctrl))
}
tabBar.tintColor = Color.green
viewControllers = navControllers
}
private func getController<TType: UIViewController>(from type: TType.Type) -> UINavigationController{
let ctrl = TType()
let navCtrl = UINavigationController(rootViewController: ctrl)
let ctrlName = String.init(describing: type.self).replacingOccurrences(of: "ViewController", with: String.empty)
navCtrl.tabBarItem.title = ctrlName
navCtrl.tabBarItem.image = UIImage(named: ctrlName)
navCtrl.navigationBar.topItem?.title = ctrlName
return navCtrl
}
}
Those UIViewControllers are created using "add Cocoa Touch Class" option and have assigned *.xib files with some minimum design (see one more image)
Any help regarding why all screens become black when multiple (2 and more) NavigationControllers added to TabBarController would be highly appreciated.
Thanks
Clearly you forget how to init the UIViewControllers with xib file:
private func getController<TType: UIViewController>(from type: TType.Type) -> UINavigationController{
let ctrl = TType(nibName: String.init(describing: type.self), bundle: nil)
let navCtrl = UINavigationController(rootViewController: ctrl)
First if you come from any screen then dont insert navigationbar in between that viewcontroller and tabbarcontroller and when you jump to tabbarcontroller set as rootview controller and any tab you want to open than place navigation controller in between. means dont open tabbar controller with navigationbar heirarchy but when u want to open controller with tabs then place navigation controller in between.
In didReceiveRemoteNotification I have
let storyboard = UIStoryboard(name: "RAV", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "AdvanceViewController") as! AdvanceViewController
window?.rootViewController = vc
Inside the app when I navigate to AdvanceViewController the tabBar works, but when I'm open by push, it does not appear.
The Push Works, what does not work it's tabBar
Does anyone know how to do fix it?
The tab bar does not display because you instantiated the view which lies in the tab bar as the root view controller, but that would mean that it is the root controller now, not the tabbar (but you want the tabbar to be the root view controller). What you need to do is set the root view controller to the tab bar and then set the tab bar to the correct index.
I am developing an iOS app in Swift 2.3, XCode 8.0. My app has 4 tabs and to each of the tabs - 4 different View Controllers are connected. Inside the 4 View Controllers I have embedded Navigation controllers.
My requirement is that when a user selects tab 1 -> goes to ViewController 1 -> Next the user can go to 2nd View Controller as there is a navigation controller.
But when the user selects the second tab and then comes back to the first tab, instead of showing the first View Controller that is directly attached to tab 1, the View Controller that was last opened with the back button is shown.
How can I move directly move to the View Controller attached to the first tab?
The embedded navigation controller should automatically keep track of the stack for you regardless of which tab the user is in with the tab bar controller. Make sure each tab has its own navigation controller embedded into it instead of having the entire tab bar controller embedded in one navigation controller.
For example, if you implemented this programmatically:
let viewController1 = UIViewController()
let navigationController1 = UINavigationController()
navigationController1.setViewControllers([viewController1], animated: true)
let viewController2 = UIViewController()
let tabBarController1 = UITabBarController()
tabBarController1.setViewControllers([navigationController1, viewController2], animated: true)
Here the navigationController1 will keep track of which viewController is on top of the stack regardless of where the user is in the tab bar controller.
Thanks, I found the solution -
This works for me -
In my main tab bar viewController, I made it a delegate of both UITabBarController, UITabBarControllerDelegate and then implemented the didSelectViewController method of UITabBarControllerDelegate as below -
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
let index : Int = (tabBarController.viewControllers?.indexOf(viewController))!
let navigationController = viewController as? UINavigationController
navigationController?.popToRootViewControllerAnimated(false)
}
I am just starting out, and would like to use storyboard to create the constraints and positioning of my UI elements, and handle all navigation in code.
One of the first things that I wan to do is create a tab bar and navigation bar programatically and load a viewcontroller with a xib in between them.
I was able to load an empty tableview controller between the navigation bar and the tab bar, as I wanted.
On my custom controller with layout specified in a .xib file, the layout elements from the xib are displayed, the tab bar is working fine, but navigation bar does not show like it does for the tableview controller tab section.
(In appdelegate)
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let sampleViewController: TestViewController = TestViewController(nibName: "TestViewController", bundle: nil)
let navigationBar = UINavigationController(rootViewController: sampleViewController)
let genericTableViewController = TableViewController()
let navigationBar2 = UINavigationController(rootViewController: genericTableViewController)
let tabBarController = UITabBarController()
let controllers = [navigationBar, navigationBar2]
tabBarController.viewControllers = controllers
navigationBar.tabBarItem = UITabBarItem(
title: "Test",
image: UIImage(named: "ico_settings"),
tag: 1)
navigationBar2.tabBarItem = UITabBarItem(
title: "Just Tableview",
image: UIImage(named: "ico_user"),
tag: 1)
self.window!.rootViewController = tabBarController
self.window!.makeKeyAndVisible()
I have a UINavigationController and a UIViewController as its root view controller.
let rootVC = Page1ViewController() // extends UIViewController
let nav = UINavigationController(rootViewController: rootVC)
presentViewController(nav, animated: true) { () -> Void in
}
The problem is that the content of the rootVC appears under the navigation bar. I tried:
nav.navigationBar.translucent = false
this worked but I want the navigation bar to be transparent and I want the content of the rootVC not appear behind the navigation bar.
I also tried:
nav.edgesForExtendedLayout = UIRectEdge.None
but this does not change anything.
How can I get a transparent navigation bar where the content scrolls under it when scrolling but when loading the content should not appear under the navigation bar?
Remove this line:
nav.edgesForExtendedLayout = UIRectEdge.None
And add this line in your viewDidLoad from Page1ViewController
self.edgesForExtendedLayout = UIRectEdge.None
Hope it helps. Let me know if it doesn´t work and I will help you.
To address your issue of a blackbox now appearing underneath your UINavigationBar you need to set the presentation context properly. Try setting definesPresentationContext on your UINavigationController to NO. This will then use the UIWindow or parent of your UINavigationController as the presentation context.