How to overcome the effect of navigation bar - iphone

In my application i want to hide the navigation bar when i m going to previous view and i m hiding navigation bar in viewwiilldisaapper method but the effect is remaining mean it gives the white screen at the time of pop..
so can any one tell me the solution for it.
Thanks to all.

you need to hide navigation bar on viewWillAppear of that page by using
- (void) viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES animated:animated];
[super viewWillAppear:animated];
}
- (void) viewWillDisappear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO animated:animated];
[super viewWillDisappear:animated];
}
if you need to hide navigation bar in certain condition then you need to put proper if else statements, so use this info with your logic according to your condition.
And view is automatic adjusted so no white space you get after pop but you get at time of pop.(no solution for this).

(void) viewWillDisappear:(BOOL)animated
{
if (self.navigationController.topViewController != self)
{
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
[super viewWillDisappear:animated];
}

I think it happen due to customize navigation bar. Is this right?
If you have customize navigation bar then you need to resize your upcoming
view. It doesn't matter how would you hide it.

Related

iOS Navigation bar smooth transition

I have an app where the first screen (the menu for the app) does not need a navigation bar BUT the rest of the app does.
The code I am using works fine in the sense that the navigation bar is not present on the menu screen and is present elsewhere in the app BUT the BIG PROBLEM is that once you go back to the menu the navigation bar appears for about a split second and then disappears.
That is NOT a very smooth transition.
How do I make the transition SMOOTHER so that the navigation bar DOESN'T even appear for a second when I go back to the menu screen?
Here is the code that I am using:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
return self;
}
- (void)viewDidLoad {
[self.navigationController setNavigationBarHidden:YES animated:NO];
[super viewDidLoad];
}
-(void) viewDidAppear: (BOOL)animated {
[[self navigationController] setNavigationBarHidden:YES animated:NO];
[super viewDidAppear:animated];
}
Try like this,
-(void) viewWillAppear: (BOOL)animated {
[super viewWillAppear:animated];
[[self navigationController] setNavigationBarHidden:YES animated:YES];
}
Hope it may helps you...
In your first view controller:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
In your second view controller (not needed, but good practice for code clarity) :
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
set this in the view did disappear in first screen of the app
[[self navigationController] setNavigationBarHidden:NO animated:YES];
and this in viewdiddisappear of the second VC
[self.navigationController setNavigationBarHidden:YES animated:NO];
Instead of hiding and showing the navigation bar, you can update the alpha for the navigation bar. It will animate smoothly during the transition. For the view controller with transparent nav bar, instead of modifying the nav bar, create a navbar (or just the back button and title etc.) manually in the second controller's view. We will then hide the navbar when transitioning from first view controller to the second one.
On your second controller's viewWillDisappear and on your first view controller's viewWillAppear:, set the navigation bar alpha to zero using self.navigationController.navigationBar.alpha = 0;. Since this is in animation block, this will make the navigation bar disappear during the push animation.
Set the alpha back to one in second controller's viewWillAppear and first controller viewWillDisappear.

setNavigationBarHidden:YES doesn't work with the searchDisplayController

I'm using the following code to hide my navigationBar in the detailViewController(my second view),
and it works perfectly fine when I tap any of my object from the MasterViewController(my first view).
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
However, when I filter the table list in the masterViewController using searchDisplayController
and tap any object from the result, the navigationBar in the detailView doesn't get hidden...
Do I have to do any extra work to hide the navigationBar if I use the searchDisplayController?
for Debug, I set the break point on the line of setNavigationBarHidden:YES, and even if
I go to the detailViewController via search result, the application hits the line..
you shuold put [self.navigationController setNavigationBarHidden:YES]; in viewWillLayoutSubviews function.like this:
- (void) viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
[self.navigationController setNavigationBarHidden:YES];
}
it works.
You should try this method:
In that controller, where you declared UISearchController *searchController, you should implement two methods (only for example):
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// if you want to hide Navigation Bar when searchController will become active
_searchController.hidesNavigationBarDuringPresentation = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
_searchController.hidesNavigationBarDuringPresentation = NO;
}
The code above may have differences. Main point in hidesNavigationBarDuringPresentation property (iOS 8.0 and later). Try to play with it, and turn to hidesNavigationBarDuringPresentation = NO before pushing a new controller. After this manipulations I took profit: when pushed UIViewController, setter setNavigationBarHidden:YES become working
if you want to hide Navigation bar then, In your MainWindow xib uncheck "Shows Navigation Bar" attributes of Navigation Controller.
This Will hide the Navigation Bar in your Whole Project. If you want to Show Navigation Bar in any Controller set NavigationBar Hidden = NO in ViewDidLoad Method of that Controller.
you should hack search display controller in some way to hide its built in navigationBar.
here is the answer:
https://stackoverflow.com/a/6337037/1348121
This
- (void) viewWillLayoutSubviews
causes layout problems, so i used code below. Works fine for me.
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO];
}

navigation bar is visible when i navigate back to main view

i am using this in my view didlod
[self.navigationController setNavigationBarHidden:YES];
it hides when applicationn launches but when i navigate to next screen and come back to main view is not hide it navigation bar...
why is it like that?
should i add any thing ?
....
This works for me:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
You wouldn't then need the one in viewDidLoad.
If it's not clear from that change, the reason your original code didn't work is that the view may be kept in memory even if it is not on screen - so need to hide / display the navigation bar each time the view is brought on or off screen.
viewDidLoad only fires the first time it loads your view. viewWillAppear fires every time.

Prevent a UISearchDisplayController from hiding the navigation bar

Whenever a user begins editing a UISearchDisplayController's search bar, the search controller becomes active and hides the view's navigation bar while presenting the search table view. Is it possible to prevent a UISearchDisplayController from hiding the navigation bar without reimplementing it?
I just debugged a bit into UISearchDisplayController and found that it's calling a private method on UINavigationController to hide the navigation bar. This happens in -setActive:animated:. If you subclass UISearchDisplayController and overwrite this method with the following code you can prevent the navigationBar from being hidden by faking it to be already hidden.
- (void)setActive:(BOOL)visible animated:(BOOL)animated;
{
if(self.active == visible) return;
[self.searchContentsController.navigationController setNavigationBarHidden:YES animated:NO];
[super setActive:visible animated:animated];
[self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO];
if (visible) {
[self.searchBar becomeFirstResponder];
} else {
[self.searchBar resignFirstResponder];
}
}
Let me know if this works for you. I also hope this won't break in future iOS versions... Tested on iOS 4.0 only.
The new UISearchController class introduced with iOS 8 has a property hidesNavigationBarDuringPresentation which you can set to false if you want to keep the navigation bar visible (by default it will still be hidden).
The simplest solution and no hacks.
#interface MySearchDisplayController : UISearchDisplayController
#end
#implementation MySearchDisplayController
- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
[super setActive: visible animated: animated];
[self.searchContentsController.navigationController setNavigationBarHidden: NO animated: NO];
}
#end
The above answers didn't work quite right for me. My solution is to fool the UISearchDisplayController into thinking there wasn't a UINavigationController.
In your view controller, add this method
- (UINavigationController *)navigationController {
return nil;
}
This had no untoward side effects for me, despite seeming like a really bad idea... If you need to get at the navigation controller, use [super navigationController].
Since iOS 8.0 the same behavior can be achieved by setting the UISearchController's self.searchController.hidesNavigationBarDuringPresentation property to false.
The code in Swift looks like this:
searchController.hidesNavigationBarDuringPresentation = false
Tried this a different way, without subclassing UISearchDisplayController. In your UIViewController class where you set the delegate for UISearchDisplayController, implement searchDisplayControllerDidBeginSearch: and add use
[self.navigationController setNavigationBarHidden:NO animated:YES];
Did the trick for me, hope that helps.
I ran into this while tackling a slightly different problem. While using UISearchDisplayController, I want the search bar to be in the navigation bar (not under).
It's not hard to put the search bar in the navigation bar (see UISearchBar and UINavigationItem). However, UISearchDisplayController assumes the search bar is always underneath the navigation bar and (as discussed here) insists on hiding the navigation bar when entering search, so things look awful. Additionally, UISearchDisplayController tints the search bar lighter than normal.
I found a solution. The trick is to (counter-intuitively) unhook UISearchDisplayController from controlling any UISearchBar at all. If using xibs, this means deleting the search bar instance, or at least unhooking the outlet. Then create your own UISearchBar:
- (void)viewDidLoad
{
[super viewDidLoad];
UISearchBar *searchBar = [[[UISearchBar alloc] init] autorelease];
[searchBar sizeToFit]; // standard size
searchBar.delegate = self;
// Add search bar to navigation bar
self.navigationItem.titleView = searchBar;
}
You will need to manually activate the search display controller when the user taps the search bar (in -searchBarShouldBeginEditing:) and manually dismiss the search bar when the user ends searching (in -searchDisplayControllerWillEndSearch:).
#pragma mark <UISearchBarDelegate>
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
// Manually activate search mode
// Use animated=NO so we'll be able to immediately un-hide it again
[self.searchDisplayController setActive:YES animated:NO];
// Hand over control to UISearchDisplayController during the search
searchBar.delegate = (id <UISearchBarDelegate>)self.searchDisplayController;
return YES;
}
#pragma mark <UISearchDisplayDelegate>
- (void) searchDisplayControllerDidBeginSearch:(UISearchDisplayController
*)controller {
// Un-hide the navigation bar that UISearchDisplayController hid
[self.navigationController setNavigationBarHidden:NO animated:NO];
}
- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController
*)controller {
UISearchBar *searchBar = (UISearchBar *)self.navigationItem.titleView;
// Manually resign search mode
[searchBar resignFirstResponder];
// Take back control of the search bar
searchBar.delegate = self;
}
Really nice solution, but it was crashing my app under iOS6. I had to make the following modification to get it work.
#implementation ICSearchDisplayController
- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
if (visible == YES) {
[super setActive:visible animated:animated];
[self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO];
} else {
[super setActive:NO animated:NO];
}
}
This seem to solve it for me. Tested in both iOS5/6.1. No visual issues that I could see.
- (void)viewDidAppear
{
[super viewDidAppear];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardWillAppear:(NSNotification *)notification
{
[self.navigationController setNavigationBarHidden:NO animated:NO];
}
-(void)viewDidLayoutSubviews{
[self.navigationController setNavigationBarHidden:NO animated:NO];
}
iOS 7 screws things up a bit... for me this worked perfectly:
/**
* Overwrite the `setActive:animated:` method to make sure the UINavigationBar
* does not get hidden and the SearchBar does not add space for the statusbar height.
*
* #param visible `YES` to display the search interface if it is not already displayed; NO to hide the search interface if it is currently displayed.
* #param animated `YES` to use animation for a change in visible state, otherwise NO.
*/
- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[self.searchContentsController.navigationController setNavigationBarHidden:YES animated:NO];
[super setActive:visible animated:animated];
[self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
The reason for show/hide the statusbar
I think the best solution is to implement the UISearchDisplayController yourself.
It's not that difficult. You only need to implement UISearchBarDelegate for your UIViewController and include a UITableView to display your search results.
#Pavel's works perfectly well. However, I was trying to get this into a UIPopoverController and the text in the field gets pushed slightly when the search bar's text field becomes the first responder, and that looks a bit ugly, so I fixed it by calling the super method with
animated set to NO.
As jrc pointed out "unhook UISearchDisplayController from controlling any UISearchBar" seems to work for me. If I pass nil as a parameter when creating UISearchDisplayController the navigation bar stays visible at all times:
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:nil contentsController:self];
I was adding custom navigation bar on my ViewController which was getting hidden on search, a quick but not so good fix was
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
[self.view addSubview:_navBar];
}
_navBar is UINavigationBar added programmatically,
doing this helped me navigation bar from hiding.
Just wanted to add to stigi answer. When you cancel search and start search again - search results table won't be react to touches so you need to add next line
self.searchResultsTableView.alpha = 1;
So updated code looks next way
- (void)setActive:(BOOL)visible animated:(BOOL)animated;
{
if(self.active == visible) return;
if (visible) {
[self.searchContentsController.navigationController setNavigationBarHidden:YES animated:NO];
[super setActive:visible animated:animated];
[self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO];
self.searchResultsTableView.alpha = 1;
[self.searchBar becomeFirstResponder];
} else {
[super setActive:visible animated:animated];
[self.searchBar resignFirstResponder];
}
}

Keeping UINavigationController's navigationBar hidden after UISearchDisplayController selection

I have a UISearchDisplayController setup with a UITableViewController which is nested inside a UINavigationController. When a selection of a cell is made, UITableView's didSelectRowAtIndexPath method is triggered, which pushes a new view to the parent navigation controller. This new view should have the navigation bar hidden on entry.
[[self navigationController] setNavigationBarHidden:YES animated:NO];
I use this line in the didSelectRowAtIndexPath method to hide the navigation bar. This works fine when a row is selected not using the search controller, but is overridden when selecting a search result. It seems the UISearchDisplayController takes it in its right to un-hide the navigationBar sometime after the row is selected.
If I move the setNavigationBarHidden call into the target view's viewWillAppear method, results are similar. I can make it work by placing the hide call in viewDidAppear, but this makes for a very awkward transition effect which feels jumpy and out of place. I would like to make the navigationBar already hidden before the new view slides on to the screen.
Does anyone know where the unhiding of the navigationBar is occurring, and/or any way I can override this behaviour?
This may not be the most elegant solution, but I believe it does exactly what you'd want it to. I came across a similar problem, and my solution was to have a method which hides the navigation bar, which is called after a delay of 0 seconds as follows.
The method that is called is:
-(void) hideNavBar {
if (self.navigationController.navigationBar.hidden == NO)
{
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
}
Then in the viewDidLoad method, I have the following:
[self performSelector:#selector(hideNavBar) withObject:nil afterDelay:0.0];
This works and removes the navigation bar in one instantaneous swoop. You can amend the delay time if you want the animation or for it to be removed after a delay. I tried [self hideNavBar] but that simply did not work, so sticking to what I have above.
Hope this helps, and if someone has a more elegant solution, I'm interested!
Ok, this bugged me for the a couple of hours, but I finally got it to work! The problem seems to be that the UISearchDisplayController keeps track of whether or not it has hid the navigation bar, and if it has, it restore it, after the view has been dismissed. That is why with many of the answers above you see the tail end of the animation of the bar hiding itself when the new view is pushed. However, by tricking the search display controller we can change this behavior.
First: Subclass The UISearchDisplayController
Following the answer on how to keep a navigation controller from hiding, found here, I altered the code, to keep the navigation bar hidden:
- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
if(self.active == visible)
return;
[self.searchContentsController.navigationController setNavigationBarHidden:YES animated:YES];
[super setActive:visible animated:animated];
if (visible)
[self.searchBar becomeFirstResponder];
else{
[self.searchBar resignFirstResponder];
[self.searchContentsController.navigationController setNavigationBarHidden:NO animated:YES];
}
}
Note we hide the navbar before we call the super setActive function. This seems to keep the super class from trying to hide the nav bar and consequently, from trying to restore it ater item selection. Now when the controller becomes active, the bar will be hidden like normal. Also note that we restore the navigation bar when the searchBar resigns first responder. This will restore the bar if we cancel out of the controller.
Second: Hide Navigation Bar When Exiting
If we hide the navigation bar in the view will disappear, it will be hidden:
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
Third: Hide Navigation Bar When Returning
The only problem now is that if we select a row from the filtered tableview, when we return, the navigation bar will be visible. To fix this we need to put a check in view will Appear:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if(self.isFiltered){
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
}
While this feels like a huge hack, it does the trick and I could see no better way of doing it.
Bumped into the same problem, managed to get it working smoothly with this ugly hack:
- (void) viewWillDisappear: (BOOL) animated
{
if (searchController_.active)
{
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
self.navigationController.navigationBar.tintColor = nil;
}
[super viewWillDisappear: animated];
}
- (void) viewWillAppear: (BOOL) animated
{
if (searchController_.active)
{
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
}
[super viewWillAppear: animated];
}
I had the same problem: my view has the navigation bar hidden by default and here's the way to keep it hidden:
-(void) viewWillLayoutSubviews{
if (self.navigationController.navigationBar.hidden == NO)
{
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
}
This way the navigation bar doesn't appear even after the search bar has been used.
- (void) viewWillDisappear: (BOOL) animated
{
// self.searchOn property tell if full screen search is enabled
//if (self.searchOn)
//{
[self.navigationController setNavigationBarHidden:NO animated:NO];
//}
[super viewWillDisappear: animated];
}
- (void) viewWillAppear: (BOOL) animated
{
//if (self.searchOn)
//{
[self.navigationController setNavigationBarHidden:YES animated:YES];
//}
[super viewWillAppear: animated];
}