I am pretty new to iOS. I have a UIViewController with an embedded UINavigationController and I am hiding the bar on the first View by adding this code to the viewDidLoad method:
if (self) {
self.navigationController.navigationBarHidden = YES;
self.navigationItem.title = #"";
}
Then I connected this View to another UIViewController with a seque (triggered by a button on the first View) and added this code to it's viewDidLoad method:
if (self) self.navigationController.navigationBarHidden = NO;
Everything works fine at first. When I load the app the navbar is gone, when I navigate to the second (child) view, the bar is there. Then when I hit the back button, it goes back to the first (parent) view but the NavBar is back.
I tried adding naming the seque "BackToMain" and adding a prepareSeque method which I placed in the second view:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"BackToMain"]) {
UINavigationController *parentNavigationController = segue.destinationViewController;
parentNavigationController.navigationController.navigationBarHidden = YES;
}
}
But it never gets called.
Ideally I would put a method in the parent view to simply hide the NavBar every time it gets displayed. Something like:
- (void)processIncomingSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if (self) self.navigationController.navigationBarHidden = NO;
}
But I really don't know if that is possible.
Put the following code in your view controller which should not have a UINavigationBar.
- (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];
}
Related
I'm trying to push a view controller with a visible navigation bar from a view controller with a hidden navigation bar.
I tried all sorts of combinations of [[self navigationController] setNavigationBarHidden:YES animated:NO]; in viewWillAppear, viewDidAppear, viewWillDisappear... etc.
// First View Controller
#implementation FirstViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[self navigationController] setNavigationBarHidden:YES animated:NO];
NSLog(#"[%# viewWillAppear]", self);
}
#end
// Second View Controller
#implementation SecondViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[self navigationController] setNavigationBarHidden:NO animated:NO];
NSLog(#"[%# viewWillAppear]", self);
}
#end
Nothing worked. I also tried custom code to "animate" a push and pop, which works, BUT I lose the edge swipe and view panning. Before I dig deeper, just want to make sure I'm not reinventing the wheel.
The Starbucks app is what I'm trying to mimic.
The root view controller of the app (the dark background view) is full screen and notice how it doesn't have a UINavigationBar. But when you tap on one of the buttons, it pushes on a view controller (the light background view) WITH a UINavigationBar. From there, if you tap the "back" arrow, it view controller pops with the navigation bar. Interactive pop swipe gesture also works.
It is possible without hacking together a solution by yourself. Here is what you do:
Your root viewController:
#implementation ViewController
....
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
#end
And the pushed viewController:
#implementation SecondViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
#end
This will do. It also keeps the interactive transition working ;)
I find it disturbing, however, that this type of functionality is not documented at all by apple. - You can also hide and show toolbars with these 'call-points' (inside viewWillAppear:)
EDIT
I just realized that this is the same code you wrote in your question. Please test it again. I am 100% sure that this works - I used this functionality in one of my apps, too.
Please also note that my code does use animated:animated instead of your animated:NO. This may be the crucial point here :)
I just set up two view controllers to test this back and forth.
#interface VC1 ()
#end
#implementation VC1
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
}
#end
and a second
#import "ViewControllerTwo.h"
#interface ViewControllerTwo ()
#end
#implementation ViewControllerTwo
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = NO;
}
#end
VC1 is embedded in a navigationController (which is the root controller for the app), with a button that navigates to ViewControllerTwo. I have a push segue from VC1 -> ViewControllerTwo, this method works. When I tap on the button, the view controller is visible on ViewControllerTwo, when I press back, the navigationBar is gone.
On View1 I hide the navigationBar in viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES];
}
Then I navigate to View2 where I show the navigationBar
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:NO];
self.title = #"Title";
}
But on back to View1 again, the navigationBar doesn't hide, even if I did tried to hide it after the pushViewController in View2
[self.navigationController pushViewController:View1 animated:YES];
[self.navigationController setNavigationBarHidden:YES];
I also tried to hide the navigation from viewWillAppear in View1 and it hides it, but there is an ugly delay and I don't find it as a good practice.
So can anyone help me with this issue, how can I hide correctly the navigationBar on back to View1?
The best practice to do what you want is putting bellow in your first viewController:
- (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];
}
-(void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.navigationController setNavigationBarHidden:YES];
}
The ViewController1 is not going to get allocated again and so viewDidLoad is not going to get called.
You can do it in viewWillAppear though. But if you are saying that there is a delay, you can do one more thing.
You can get the reference ofViewController1 in ViewController2. Suppose ViewController1 is the first controller in the navigation controller, then do this:
//ViewController2.m
- (IBAction)backButtonPressed:(id)sender{
ViewController1 *view1 = [self.navigationController.viewControllers objectAtIndex:0];
[view1.navigationController setNavigationBarHidden:YES];
Your code is correct, but you need to write like this:
[self.navigationController setNavigationBarHidden:YES];
first, then write
[self.navigationController pushViewController:View1 animated:YES];
See when you are pushing View2 from View2 in navigation stack than View1 doesn't gets deallocated. it is there in in the stack. So when you popping out View2 that time View1 viewDidLoad won't get called. so your code setNavigationBarHidden to hide navigation bar doesn't executes. So put that code to ViewWillAppear or ViewDidAppear because these methods gets called every time View appears.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES];
}
sorry if this is kind of a duplicate question but I have looked at many questions and haven't found the right answer.
Basically if the app is on first launch, a UIViewController with a How To screen should be loaded. If it is not first launch, it loads a Tab Bar Controller.
To do this, I have a UIViewController as the initial view controller, and then modal segues to the tab bar and uiviewcontroller
The two modal segues are called pushTabBar and pushHowToUse.
Here is the code for "firstviewcontroller"
#implementation FirstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self isFirstRun];
// Do any additional setup after loading the view.
}
- (BOOL) isFirstRun
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"isFirstRun"])
{
[self performSegueWithIdentifier: #"pushTabBar" sender: self];
return NO;
}
[defaults setObject:[NSDate date] forKey:#"isFirstRun"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"right before");
[self performSegueWithIdentifier: #"pushHowToUse" sender: self];
return YES;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"pushTabBar"]) {
GetInfoViewController *getInfoViewController = segue.destinationViewController;
NSLog(#"loadtabbar");
}
if ([segue.identifier isEqualToString:#"pushHowToUse"]) {
TutorialViewController *tutorialViewController = segue.destinationViewController;
NSLog(#"loadhowto");
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I am not sure what to push/load for the tab bar controller:
GetInfoViewController *getInfoViewController = segue.destinationViewController;
is the viewcontroller for the first tab bar I want to load.
Am I doing this correctly? Should I be using a navigation root controller?
Thanks in advance
There's no need for the FirstViewController if its only purpose is to decide which other controller to present (BTW, calling a modal segue pushToTabBar is a confusing name -- you're doing a presentation not a push). You can do the logic in the viewDidAppear method of the controller in the first tab of the tab bar controller. If it's the first launch, present the TutorialViewController modally with no animation so it will be the first thing the user sees. When the user is done with the tutorial, dismiss that controller, and you be back to that first tab view controller. You should hookup a segue from that controller at index 0 of the tab bar controller to the TutorialViewController.
You're better off doing the segue from the Tab Bar controller. That way, you can just dismiss the modal presentation and when the tutorial segue goes away, you're already at the tab bar.
I have a storyboard with a UINavigationController. The root view controller of the navigation controllers is called rootViewController.
I am trying to programmatically change the view controller (depending on other conditions) to another view controller called loginViewController.
I am trying to do this in the viewDidLoad from the rootViewController like this:
- (void)viewDidLoad
{
[super viewDidLoad];
loginViewController *viewController = [[loginViewController alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
}
I am not getting any errors but it's not working.
It just loads the navigation controller with the top nav back button but the rest of the screen is black. It's like it's not loading any view controller.
I am trying to figure out what I am doing wrong.
Any help with this would be great.
Thanks
First add loginViewController class in storyboard and and connect ViewController class to loginViewController class and give identifier name as "identifier_Name".
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSegueWithIdentifier:#"identifier_Name" sender:nil];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"identifier_Name"])
{
loginViewController *viewController = [segue destinationViewController];
}
}
If you're using storyboard, create your login view in the storyboard, link your rootVC to the loginVC with a segue, choose an identifier (for example: "goToLogin"). and then to go in the login view, and use:
[self performSegueWithIdentifier:#"goToLogin" sender:self];
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;