iOS: Set up the navigation controller to use GTScrollNavigationBar - ios

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.

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

Tab bar is hidden in iOS

I have a tab bar controller with four tabs. For one tab, i have created a new storyboard with navigation view controller and simple UIViewController. For other tabs, connected view controllers with navigation controller within the same storyboard.
When i try to launch first view controller from new storyboard from first tab, it is not showing tab bar. for others it is showing tab bar properly.
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSInteger index = [tabBar.items indexOfObject:item];
if(index == 0){
UIViewController *suggestionsViewCintroller = (UIViewController *)[[UIStoryboard storyboardWithName:#"suggestions_view" bundle:nil] instantiateViewControllerWithIdentifier:#"suggestions_view_controller"];
[self addChildViewController:suggestionsViewCintroller];
[self.view addSubview:suggestionsViewCintroller.view];
suggestionsViewCintroller.hidesBottomBarWhenPushed = NO;
[suggestionsViewCintroller didMoveToParentViewController:self];
}
}
Navigation controller configuration in storyboard :
View controller and tab bar controller are in different storyboards.
Why is it not showing tab bar in view?
I don't think that is the proper way to add a view controller to the tab bar. I think you want to sub class your UITabBarController and add the new view controller to the UITabBarController sub class in the viewDidLoad method of the sub class. You can see an example of this here. The main code is added in TabBarController (a sub class of UITabBarController)
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationController* naviController = [[UIStoryboard storyboardWithName:#"Other" bundle:nil]
instantiateViewControllerWithIdentifier:#"NavigationController"];
naviController.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Suggestions"
image:[UIImage imageNamed:#"suggestions"] tag:2];
self.viewControllers = [self.viewControllers arrayByAddingObject:naviController];
}
Edited the example to use 2 different storyboards.
Main storyboard:
Other storyboard:
Video here.
Calling 'addChildViewController' you do not add tab in UITabBarController.
This call just add child view controller over whole view of UITabBarController. So it is quit reasonable, that you can not see tabbar.
If you wish to add view controller as TAB of UITabBarController, you should use 'viewControllers' property as shown here:
https://stackoverflow.com/a/11399634/4322841
And should maybe use 'selectedIndex' property.

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

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

Resources