How to remove black space using Navigation Bar - ios

I am using Navigation controller.
In my first screen their is no need of NavigationBar. As this is the Home screen.So I am hiding it using this code:
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES];
}
When I push to new screen I am showing the NavigationBar using this code:
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController NO];
}
The problem is when I come back from other screens to my HomeScreen I am getting black screen in place of navigationBar.
Here is the problem in Image:
White color screen is my ViewController which has navigation bar and BLue one has'n Navigation bar. How can I remove the black part.

Click on the navigation controller and go to attribute inspector and uncheck the show navigation bar option as shown in the screenshot:

try this one
self.navigationItem.hidesBackButton = YES;

There is a better way to do it. All you need to do is create a subclass of UINavigationController class. Set the UINavigationControllerDelegate. Add the following method in the class.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if([viewController isKindOfClass: [SomeClass class]])
[self setNavigationBarHidden: NO];
else
[self setNavigationBarHidden: YES];
}
OR
-(void)viewWillDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}

Related

Unable to hide navigation bar that suddenly appear after popViewController in SearchDisplayCotroller

I have a problem where a navigation bar suddenly appear after popViewController is called when SearchDisplayCotroller is still active.
I am implementing a simple tableview with searching capability. Above is my storyboard.
On the first view, i've implemented as per below. So, the navigation bar will be always hidden for the first view.
- (void) viewWillAppear:(BOOL)animated{
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
Button press will push the next table view controller with the below code so that the navigation bar will be visible.
- (void) viewWillAppear:(BOOL)animated{
[self.navigationController setNavigationBarHidden:NO animated:NO];
}
Everything works fine until i implemented the below code.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.navigationController popViewControllerAnimated:YES];
}
When popViewControllerAnimated:YES while the SearchDisplayController is still active, a weird white navigation bar appears and my viewWillAppear is unable to hide the bar. Is this a bug or is there any way to prevent this bar from appearing?
The gif below may also give you the idea what did happen. Thanks!
Try this
-(void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// To check if searchDisplayController still active
if ([searchDisplayController isActive]) {
[searchDisplayController setActive:NO];
}
}
Try inactivating your UISearchDisplayController by calling setActive:NO animated:NO before you call [self.navigationController popViewControllerAnimated:YES];
https://developer.apple.com/LIBRARY/IOS/documentation/UIKit/Reference/UISearchDisplayController_Class/index.html#//apple_ref/occ/instm/UISearchDisplayController/setActive:animated:

Hiding a UINavigationBar after hitting back button

I have 3 view controllers and they are all chained together like so:
vc1 -> vc2 -> vc3
In view controller 1 and 2, I have the navigation bar hidden like so:
elf.navigationController.navigationBarHidden = NO;
The third view controller shows the navigation bar and has a back button on the left. When I hit the back button it goes to view controller 2 but the navigation bar is no longer hidden. How do I hide it again?
write this code in viewWillAppear method in viewController1 and viewController2
-(void)viewWillAppear:(BOOL)animated{
self.navigationController.navigationBarHidden = YES;
}
Thanks
Use this simple code in Third 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];
}

hiding the aView's Navigationbar push bView is OK.but pop Can see the black bars

SDK 6.1, Target 6.1, use storyboard
aView has a a UIButton. I use action segue [push] to the bView
When I click this button push bView is ok
But I pop aView have a back bars, how do I solve this problem?
aView.m
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController.navigationBar setHidden:YES];
}
bView.m
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController.navigationBar setHidden:NO];
}
I got what is your problem. You are hiding your navigation bar in the viewWillAppear: method of viewA and you are doing it without animation.
Try this
// This will add an animation like slide out. So you may won't like it.
[self.navigationController setNavigationBarHidden:YES animated:YES];
If it is not working, then add this code in bView.m
- (void)viewWillDisappear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:YES animated:NO];
[super viewWillDisappear:animated]
}

navigationController popToRootViewController, the navigation bar shows backButton and son view's title

I'm beginner for iOS dev. In my app, RootView jump to son views: A、B、C、D through buttons (defined in storyboard, push segue). i use some image to be touched so the son views can push to each other, I.E the code in B Controller:
// jump to View C
[self.navigationController pushViewController:CView animated: YES];
the code in C Controller, define backButton clicked to show RootView:
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
[self.navigationController popToRootViewControllerAnimated:NO];
}
[super viewWillDisappear:animated];
}
It's perfect to show RootView, but the top shows backButton and title which i defined in B Controller. RootView have an image title. Other view's title was set in storyboard.
What can i do now?
solved, the navigationBar also need to be poped:
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// add this pop:
[self.navigationController.navigationBar popNavigationItemAnimated:NO];
[self.navigationController popToRootViewControllerAnimated:NO];
}
[super viewWillDisappear:animated];
}

UIViewController pushViewController, show navigationBar

I have UIViewController(1) without navigationBar, and I need to push anouther UIViewController(2) that have navigationBar, and when I click Back on it, navigationBar must hide on 1 controller. I have tried uiviewcontroller delegates.
But nothing is working..
Please help..
This will show the navbar on the second screen:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = NO;
}
You will also need to hide the navbar when you return to the first screen:
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
self.navigationController.navigationBarHidden = YES;
}
I think you want the animated option. If you roll with the approaches above (self.navigationController.navigationBarHidden = value) you get some undesirable visual crumbs.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
This will push/pop smoothly with the navBar firmly attached to the appearing/disappearing view.
Place this code in first view controller
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
}
Answers from Alfie or Ader would be a disaster if you have many viewcontrollers' navigation bar hidden/show state to manage.
I just have posted code dedicated on UINavigationBar appearance management on github. check out RRViewControllerExtension, it will solve your problem gracefully.
With RRViewControllerExtension, you even don't have to #import the header file, all you have to do is just override any desired method below in your viewcontroller.
//override any of the methods below in your viewcontroller's .m file to make specific navigation bar appearance
-(BOOL)prefersNavigationBarHidden;
-(BOOL)prefersNavigationBarTransparent;
-(nullable UIColor *)preferredNavatationBarColor;
-(nullable UIColor *)preferredNavigationItemColor;
-(nullable UIImage *)preferredNavigationBarBackgroundImage;
-(nullable NSDictionary *)preferredNavigationTitleTextAttributes;

Resources