How to set NavigationController navigationItem title from ViewController loaded by Xib - ios

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;
}

Related

How to let child view controller add UISearchBar to parent's UINavigationController?

How to let child view controller modify navigation controller?
I am faced with this problem right now, and have not been able to find a solution that works, let alone one that works well.
Basically, right now I have a UINavigationController that contains a UITableViewController with a search bar in the navigation bar. So far so good - everything works well.
However, I would like to show some custom views between the navigation bar and the top of the table view at times. To do this, I figured it would be best to create another view controller (let's call it the ContainerViewController), which is presented by the navigation controller. This container view controller holds the table view controller as a child, and can insert any custom view it wishes to. The view controller hierarchy should look like this:
UINavigationController
ContainerViewController
UITableViewController (with UISearchDisplayController)
Now I am faced with a problem, since the search bar of the UISearchDisplayController should be displayed in the navigation bar (by calling didSetupSearchDisplayController), but I obviously want all logic pertaining to it kept in the UITableViewController. I need the container view controller to sort of act as a go-between here. I have tried to figure this out, but haven't managed a solution yet.
Here is how I instantiate the table view controller and add it as a child in the container view controller.
- (void)viewDidLoad {
[super viewDidLoad];
// Load view controller from storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
self.contentViewController = [storyboard instantiateViewControllerWithIdentifier:#"MyListViewController"];
self.contentViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
// Add its view to the content view
[self addChildViewController:self.contentViewController];
[self.container addSubview:self.contentViewController.view];
[self.contentViewController didMoveToParentViewController:self];
// Set up constraints
[self.contentViewController.view autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
}
In the child view controller, I have noticed that self.navigationController is nil in viewDidLoad but not in didMoveToParentViewController. So the following works:
- (void)didMoveToParentViewController:(UIViewController *)parent {
// Hide separator below navigation bar
self.navigationController.navigationBar.clipsToBounds = YES;
}
But I cannot set the search bar to display in the navigation bar, nor can I set the navigation bar title (with self.title = ...) from within the child view controller.
Would appreciate any help.
EDIT: Here is some code I use in the UITableViewController (the contained view) to create the search display controller:
UISearchBar *searchBar = [[UISearchBar alloc] init];
self.mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
// Show search bar in navigation bar (instead of in its own bar underneath)
self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
// Set self as delegate to search display controller and its table view
self.searchDisplayController.delegate = self;
self.searchDisplayController.searchResultsDelegate = self;
I have tried doing this both in viewDidLoad, where the self.navigationController is nil, and in didMoveToParentViewController, where the navigation controller exists, but it does not seem to matter. My guess is that the displaysSearchBarInNavigationBar magic happens after the child view is instantiated but before it is added as a child view (and thus gets access to the navigation controller).
can you use
self.parentViewController.navigationController
to get the reference to the navigation controller from within the container view?
This may help you find your answer. We use this to set the title of our navigation controller
self.navigationItem.title=#"Name of view goes here";
You should be able to use self.navigationItem to get a handle to the navigation bar.

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.

correct way of using UINavigationController's initWithNavigationBarClass:toolbarClass:

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;
}

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