correct way of using UINavigationController's initWithNavigationBarClass:toolbarClass: - ios

From the apple docs I understand that a UiNavigationController can be instantiated with another Uinavigationbar using initWithNavigationBarClass:toolbarClass: method. How does one correctly do this via a custom UiNavigationBar subclass and IB?

You can use it like this to initialize the navigation controller,
UINavigationController *navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[CustomNavigationBar class] toolbarClass:nil];
Here CustomNavigationBar is the custom class created by subclassing UINavigationBar. You can set the viewcontrollers by using the setViewControllers property of UINavigationController.
If you want to do this in IB, try this. Select navigation bar from objects and in the identity inspector, select the custom class for navigationbar.

In Interface Builder, you click on the navigation bar inside the navigation controller. Inspect it on the right panel, and change the custom class from UINavigationBar to your custom subclass.
In code, make sure you have imported your header file for the navigation bar subclass and write something similar to the following.
// This code assumes `MyCustomNavigationBar` is the name of your custom subclass, and that `viewController` is a UIViewController object created earlier.
// To create the containing navigation controller
UINavigationController *navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[MyCustomNavigationBar class] toolbarClass:[UIToolbar class]];
// To set the root view controller in the navigation controller
navigationController.viewControllers = #[viewController];
The code above informs UIKit to create a UINavigationController with navigation bars of subclass MyCustomNavigationBar. Then, it sets the root view controller to the object stored in the variable viewController.

Just mashing up Benjamin Mayo's answer here for your general subclass
- (UINavigationController *)initWithRootViewController:(UIViewController *)rootViewController navigationBarClass:(Class)navigationBarClass {
self = [super initWithNavigationBarClass:navigationBarClass toolbarClass:UIToolbar.class];
if (self) {
self.viewControllers = #[rootViewController];
}
return self;
}

Related

How to set NavigationController navigationItem title from ViewController loaded by Xib

I've a UIViewController which loads it's view form a Xib file. In my AppDelegate.m I would like to initiate my rootViewController with that myViewController and I would like that the title of the navigtionItem is set by taking the NavigationBar view which is part of the Xib file of the myViewController:
MyViewController *myViewController = [[MyViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
According to the documentation
Each time the top-level view controller changes, the navigation controller updates the navigation bar accordingly.
In my setup this seems not to happen or I haven't indicated that my NavigationBar view is actually the navigationBar to consider.
How can I tell the NavigationController to update it's navigationItem content (title, left and right bar buttons) according the NavigationBar view which is part of the Xib file from myViewController?
Update
My Xib layout looks like this:
I know I can set those items in code by:
self.navigationItem.titleView = ...;
self.navigationItem.leftBarButtonItem = ...;
But I want them do design in the Xib file of the ViewController and they should be used in the NavigationController.
In ViewWillAppear method in your ViewController set-
self.navigationItem.title = #"Title name";
Also in the same way you can set self.navigationItem.leftBarButton and RightBarButton items
Adding a UINavigationBar view to the main UIView doesn't do what you think it does. It merely adds a navigation bar view there in the view, it does not tie into the UINavigationController.
If you use a storyboard, you can add the UINavigationItem as a child of the UIViewController. But since you are not using a storyboard, the easiest way to do it is to update the navigationItem in viewDidLoad.
Sidenote: Apple does not recommend using viewDidLoad to setup the navigation item. If you really want to do it the proper way, you can for example do it in the navigationItem getter.
- (UINavigationItem*)navigationItem
{
// get super navigation item
UINavigationItem *navItem = [super navigationItem];
// do stuff
return navItem;
}

ios: TableViewControl in TabViewControl the navigation bar overlaps table view

The root View View controller is Navigation controller and its first level is a TabViewController.One of the tab item is a TableViewController.
Here is the relationship:
However the navigation bar overlap the table view:
I have also set simulated metrics,So what can be the problem??
Thanks for any help.
Simulated metrics are just that, simulated. They do not actually apply to the compiled product.
To fix this, I find it easiest to set the edgesforextendedlayout with the various values of edge values. Usually all but the top.
The rootViewController should be the UITabBarController. Follow this code:
1.Make the UITabBarController the rootViewController in the application delegate or in your main.storyboard set it as the initial View Controller.
2.In the UITabBarController.m place this code there to create the UINavigationController with a UIViewController embeded inside of it.
//Inside UITabBarController.m
- (void)viewDidLoad {
[super viewDidLoad];
UIViewController *vc = [[UIViewController alloc]init];
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:vc];
self.viewControllers = #[navCon];
}
Get rid of the navigation controller in the beginning and, instead, embed the tableviewcontroller inside a navigation controller.
(Select the view controller -- Click "editor" > "embed in" > "navigation controller").
Make sure the tab bar controller is the root view controller as well
This will also fix the overlapping issue

UINavigationController Title(s) and buttons are not displayed within UITabBarController

Im having difficulty getting the titles of the navigation bars to display along with the buttons within my tabBarController interface. Im creating the tabBarController programmatically. Here is the screenshot for reference.
I have tried putting self.navigationController.navigationBarHidden = YES; within the alloc/init method of the tabBarController which is allocated in the appDelegate and set as the windows rootViewController. I've also tried to set its title with this code self.navigationController.title = [[self.viewControllers objectAtIndex:self.selectedIndex]title ];. I have also tried using the same code within the viewDidLoad method of my tabBarController class. Within the UITabBarController's alloc/init method I do have this code to set the nav controllers that I have added to the viewControllers array.
UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:contactsTblView];
nav2.title = #"Contacts";
nav2.navigationItem.title = #"Contacts";
nav2.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:2];
nav2.delegate = self ;
The title that appears in the navigation bar is the title of the currently showing view controller (the top of the navigation controller's stack). You should set the title of the individual view controllers embedded in the navigation controller, not the navigation controller itself.

iOS: Set up the navigation controller to use GTScrollNavigationBar

I am using StoryBoard, I've added a UITableViewController and then embedded it with navigation bar.
Now, I want to implement GTScrollNavigationBar
in my application, in Usage section, it says : Set up the navigation controller to use GTScrollNavigationBar
how can I do that in my .m ?
Thnaks in advance
Set up the navigation controller to use GTScrollNavigationBar:
#import "GTScrollNavigationBar.h"
self.navController = [[UINavigationController alloc] initWithNavigationBarClass:
[GTScrollNavigationBar class] toolbarClass:nil];
[self.navController setViewControllers:#[self.mainViewController] animated:NO];
In your view controller which has a UIScrollView, e.g. UITableViewController, set the UIScrollView object to the GTScrollNavigationBar in viewWillAppear: by:
self.navigationController.scrollNavigationBar.scrollView = self.tableView;
To unfollow the scrollView, simply set scrollView property to nil
self.navigationController.scrollNavigationBar.scrollView = nil;
Implement scrollViewDidScrollToTop: in the view controller to reset the navigation bar's position
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
[self.navigationController.scrollNavigationBar resetToDefaultPosition:YES];
}
Source.

Setting view controller for titleView of UINavigationItem

I am creating a view controller that is pushed to the UINavigationController. I need a customized navigation bar, therefore I have overridden navigationItem method of my view controller. In a titleView (property of UINavigationItem) I need a custom view that holds two buttons. Question: how to apply view controller for those two buttons?
View containing two buttons is defined in CustomTitleViewController.xib, whereas its view controller is defined in CustomTitleViewController class.
This is my way of returning the navigation item (MainViewController.m):
- (UINavigationItem *)navigationItem
{
UINavigationItem *navItem = [[UINavigationItem alloc] init];
UIViewController *customTitleViewController =
[[CustomTitleViewController alloc] initWithNibName:#"CustomTitleViewController"
bundle:[NSBundle mainBundle]];
navItem.titleView = [customTitleViewController view];
return navItem;
}
The view is shown on the navigation bar, as expected. However, tapping the button crashes the application (EXC_BAD_ACCESS).
Error message: message sent to deallocated instance 0x6e53850.
Any ideas?
Basically, you need to retain pointer to viewController as well as to its view. Just create strong property on your UINavigation controller subclass. Coz what you are doing here is instantiating new controller each time navigation item is called and realising it at the end of the function. Here is a very quick crude fix:
https://www.dropbox.com/s/y6ltdyj951ioncd/Navigating.zip
be sure not to reinstantiate VC all the time and keep pointer to it.
Hope this helped.

Resources