UINavigationBar dims down when entering search - uinavigationcontroller

I've made an empty view controller wrapped in a navigation controller, and added a search bar using the navigationItem.searchController. A weird thing though, when tapping the search bar the animation works well but the navigation bar dims down as the entire screen.
This is NOT how it works on Settings and other places, i.e. the color of the navigation bar should stay the same.
Any ideas?
Some code:
- (void)viewDidLoad {
[super viewDidLoad];
UISearchController* searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
searchController.searchResultsUpdater = self;
self.navigationItem.searchController = searchController;
...
}

Simply set dimsBackgroundDuringPresentation or obscuresBackgroundDuringPresentation to NO.

In your viewDidLoad method, add:
self.definesPresentationContext = YES;

Related

How do I set up a Tab Bar Controller within an existing Navigation Controller?

I'm pretty new to iOS. I'm building an app and am running into an issue. I have a navigation controller with a table view controller atop its stack. When I select a row in that table view controller, what I'd like to see is a collection view with the following:
The nav bar with the name that appears on the selected cell as the navigation item title.
A collection view as the main interface
a tab bar with the collection view, and an imagePickerController
Here's what my code looks like:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NewTabBarController *tbc = [[NewTabBarController alloc] init];
UIImagePickerController *takeAPicture = [[UIImagePickerController alloc] init];
UITabBarItem *tabItem = [takeAPicture tabBarItem];
[tabItem setImage:[UIImage imageWithContentsOfFile:#"CameraIcon.jpg"]];
[tabItem setTitle:#"Take a photo!"];
UICollectionViewFlowLayout *photoFlow = [[UICollectionViewFlowLayout alloc] init];
PhotoCollectionViewController *photoHub = [[PhotoCollectionViewController alloc] initWithCollectionViewLayout:photoFlow];
[tbc setViewControllers:[NSArray arrayWithObjects:photoHub, takeAPicture, nil]];
NSArray *items = [[items accessor] allItems];
Item *item = [items objectAtIndex:[indexPath row]];
[photoHub setItem:item];
[photoHub useLayoutToLayoutNavigationTransitions];
[[self navigationController] pushViewController:tbc animated:YES];
}
Then in my PhotoCollectionViewController implementation I have:
#syntesize item;
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationItem *itemHeader = [self navigationItem];
[itemHeader setTitle:[item itemName]];
UITabBarItem *tabItem = [self tabBarItem];
[tabItem setImage:[UIImage imageWithContentsOfFile:#"itemImage.jpg"]];
[tabItem setTitle:[NSString stringWithFormat:#"Photos of %#", [item itemName]]];
}
My problem is that when I select the cell, The collection view loads, and I can see the cells I have set up in the collection view, but the nav bar item has no title, and the tab bar item has "Photos of (null)" and no image. The "Take a photo!" text appears, but the image does not.
Do you guys have any idea how I can restructure this to make everything flow correctly. I must be doing something wrong in the way I'm utilizing tab and nav controllers.I don't want there to be any tabs until this stage in the app, which is 3 or 4 VCs in already. Should I be using a tab bar controller from the App Delegate onward?
The problem here is that you are pushing tab bar controller onto a navigation controller stack. The view controllers of a tab bar will have a navigation item, but their navigation items aren't shown when the view controller is on screen. Instead, the tab bar controller's navigation item is on screen.
You could use self.tabBarController.navigationItem, but then each view controller will have to modify the navigation item every time it's brought on/off screen, which is really messy.
If you're going to use a UITabBarController, I would recommend either presenting it modally, or having it be the root view controller on your UIWindow. It's tough to get it working right as a view controller in a navigation controller's view controller stack.
Your tab bar item not showing its name is a separate issue. It's because viewDidLoad is getting called before you set your item instance, specifically it's getting called when you call [tbc setViewControllers:[NSArray arrayWithObjects:photoHub, takeAPicture, nil]];
You can confirm this by breakpointing in view did load, where you'll see that item is nil. If you haven't already, you should overload your setItem: method in PhotoCollectionViewController, and have that method also update your UI.

Custom Image in navigationbar hiding the custom navigationbar buttons

First of all I explain what i am doing. I created a navigation based application. I have to add a custom image in the navigationbar. I added the image by using -
[self.navigationController.navigationBar insertSubview:image atIndex:0];
After this i added two custom buttons left and right to the navigation bar of the same view.I have another view and on this view i also added two custom buttons left and right to the navigation bar. All is fine till now but as i navigate to my second view my custom buttons that i am adding to the navigation controller on viewwillappear doesn't show. I used this code to add custom buttons to navigation bar -
UIBarButtonItem *customHome = [[UIBarButtonItem alloc] initWithCustomView: buttonHome];
[self.navigationController.navigationItem setLeftBarButtonItem:customHome];
Please suggest what's wrong in this. :(
You're trying to set the navigationItem on the navigation controller instead of the view controller.
If you have embedded your UIViewController in a UINavigationController then in viewDidLoad of the View Controller you could, for example, make the title a UISearchBar (that you dragged to the UIViewController in Xcode IB) and put 2 buttons on the right like this:
UIBarButtonItem *componentsButton = [[UIBarButtonItem alloc] initWithTitle:#"components" style:UIBarButtonItemStylePlain target:self action:#selector(componentsButtonTapped:)];
UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc] initWithTitle:#"settings" style:UIBarButtonItemStylePlain target:self action:#selector(settingsButtonTapped:)];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:componentsButton, settingsButton, nil];
self.navigationItem.titleView = searchBar;
The issue here is the distinction between UINavigationController, which manages UINavigationBar, and UINavigationItem, which you and UINavigationController co-manage. (Don't forget the UINavigationBarDelegate protocol, which has little used but useful methods.)

Image instead of title in navigation bar of TTLauncherView

I use the TTLauncherView in my project. To have a custom navigation bar background I have subclassed the UINavigationBar in my app delegate. This works fine and all navigation bar's now have this custom style.
#implementation UINavigationBar (CustomNavBarBG)
-(void)drawRect:(CGRect)rect{
UIImage *image = [UIImage imageNamed:#"navbar_s.png"];
[image drawInRect:self.bounds];
}
#end
When navigating through the views the title appears in the middle of the bar. But on the navigation bar of the main screen, the launcher, I want to have an image instead of the title. Is this possible?
How to implement it?
you can override the default title view when you load your controller with a custom UIView, such as a UIButton:
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *logoView = [[[UIButton alloc] initWithFrame:CGRectMake(0,0,85,40)] autorelease];
[logoView setBackgroundImage:[UIImage imageNamed:#"navBarLogo.png"] forState:UIControlStateNormal];
[logoView setUserInteractionEnabled:NO];
self.navigationItem.titleView = logoView;
}
I'm actually not sure why I used UIButton here :-) maybe you can use a UIImageView instead, but this code works fine.

core-plot and navigationController

I am using core-plot sdk and trying to plot a bar chart.
I have RootViewController which extends from UINavigationController, and I am adding my viewController which render the barChart, here is code
RootViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
barChartViewController = [[BarChartViewController alloc]init];
[self pushViewController:barChartViewController animated:NO];
}
BarChartViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.title =#"BarChart";
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.4 green: 0.8 blue:0 alpha:1];
self.navigationItem.title = #"BarChart";
// and then creating the barchart and rendering it, which works fine.
}
Here the navigationBar doesn't appear and so is the title for navigationBar.
And then the following code from BarChartViewController.m
-(void)barPlot:(CPBarPlot *)plot barWasSelectedAtRecordIndex:(NSUInteger)index{
NSLog(#"barWasSelectedAtRecordIndex %d", index);
DetailViewController *detailViewController =
[[DetailViewController alloc]initWithNibName:#"DetailViewController" bundle:
[NSBundle mainBundle]];
[self.navigationController pushViewController: detailViewController animated:YES];
}
So in the above code what I am trying to do is once user selects a bar from the barChart (which is getting displayed properly), the above method gets invoked, i do see on the console the log message is getting displayed telling which barChart was selected, but the controller is not going to DetailViewController, which I am pushing here.
So as you can see I am facing two problems with the code one the navigation bar is not appearing and another one I am not able to push a new viewController to navigationController. I am guessing there is some issue with the core-plot and navigation controller.
Please help.
Thanks,
Yogesh

UITabBarController/UINavigationController missing title

I have a UINavigationController and UITabBarController visible at the same time. Both the tab bar buttons and the navigation bar take their text from the title of the currently active view.
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"View Title";
However I want the different. In fact I'd like the navigation controller title to remain the same whichever view is being displayed.
Any ideas?
Many thanks.
Try This
UITabBarItem *tItem = [self tabBarItem];
[tItem setTitle:#"Title for tabbar"];
it won't change your tabbar title when you set the navigation title like
[self setTitle:#"Title for navBar"];
but I found sometimes it changes both tabBar and naviBar
then I insert the TabBar set titling code in -(void)viewWillAppear again to prevent
such happening
Thanks
You'll have to manually assign the title name, as you are doing above, in your other view controllers.
If your view hierarchy is more than two levels deep, be wary as the back button will look a little strange to the user (unless you override it). In fact, if every view has the same title, that may look strange to the user, period...
-(void)viewDidLoad{
[self setTitle:#"Custom Title goes here"];
}
This worked for me.
According to KwangBok Lee, I have tested, the best answer would be:
self.title = #"my settings";//changes both tabbars and navigationbars title
self.tabBarItem.title = #"settings";//only changes tabbars title
What I've found is... using
self.tabBarController.title = #"navTitle";
changes the navigation bar title, while
self.title = #"tabTitle";
changes the tab bar item title.

Resources