I have an iPhone app that is based on a navigation controller.
I have a main view controller that displays a list of articles, and a detail view, where you can see one article in a UIWebView. For the detail view, I have the navigation bar on the top, and a UIToolbar on the bottom.
I'd like to auto-hide them with a slide animation (to top and bottom) and restore them when tapping the screen. I thought this would be a standard function, but couldn't find how to do it.
As a reference, this is what Stanza or the NYT app do.
Set up a method that runs this on a tap event:
if (![navigationController isNavigationBarHidden])
[navigationController setNavigationBarHidden:YES animated:YES]; // hides
else
[navigationController setNavigationBarHidden:NO animated:YES]; // shows
As for the UIToolbar, it is a UIView subclass, so you should be able to pretty easily set up a custom animation for sliding this in and out of sight.
There is also quite a useful method for UIVIewController.
- (BOOL) hidesBottomBarWhenPushed {
//hide a toolbar or whatever
return NO;
}
Try this:
BOOL hide = ![self.navigationController isNavigationBarHidden];
[self.navigationController setNavigationBarHidden:hide animated:YES];
I guess recently you can use self.navigationController.hidesBarsOnTap = true;
Related
Currently i am working in iPhone app, I have two screen like A and B, A has no navigation bar, but B has navigation bar. so i set like this.
Class A:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title=#"A";
[self.navigationController setNavigationBarHidden:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES];
}
Class B:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title=#"B";
[self.navigationController setNavigationBarHidden:NO];
}
-(void)Previousscreen
{
[self.navigationController popViewControllerAnimated:YES];
}
then i run the application, When i go to previous class like B to A at the time blue color show in B class below attached screen shot for your reference. How to fix this issue? please help me
Thanks in Advance
Set it in class B
-(void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO];
}
You'll need to use this code:
[navigationController setNavigationBarHidden: YES animated:YES]
in - (void)viewWillAppear:(BOOL)animated or later in the view lifecycle in both classes. [Avoid doing this in - (void)viewDidLoad.]
The trick here is in using the setNavigationBarHidden:animated: method (in place of the simpler setNavigationBarHidden: method). This will ensure your UI issue goes away and also any positional issues due to it.
P.S. Check the value of self.navigationController.navigationBarHidden (instead of self.navigationController.navigationBar.hidden) if you need to check if your navigation bar is hidden, at some point, in your code.
I don't think a behavior when you are hiding and showing the navigation bar dynamically as you are pushing controllers is supported.
Simple solution - hide the animation bar of the UINavigationController and if you want to show it on some controller, just add a UINavigationBar to it.
Use below line to hide navigationBarin viewWillAppear: method -
-(void)viewWillAppear:(BOOL)animated
{
self.navigationController.navigationBar.hidden=YES;
}
Try setting the navigationBarHidden: in viewWillDisAppear of class B
in class B
-(void)viewWillDisAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES];
}
Your code is okay for Hide and Unhide the navigationBar. The problem is that you're hiding Class A's navigationBar in viewWillAppear: which is called just before appearing the view so before loading the Class A view navigationBar is being hidden each time.
And if we talk about your blue color i think it is your window color. Because after hidden the navigationBar there will be a space above your self.view which height is 44.0. So there are three options to fixed it.
Hide Class A's navigationBar in Class A's viewDidAppear: method.
Set your window color what you want to show.
You can add an image to window background in which at top of image make a navigationBar same as Class B's navigationBar so when the original navigationBar will be hide it will see.
I've had to solve this recently and I found that it was necessary to call setNavigationBarHidden:NO immediately after pushViewController: and setNavigationBarHidden:YES immediately after popViewController:, with animated YES in each call.
So, when pushing:
[nc pushViewController:classBView animated:YES]
[nc setNavigationBarHidden:NO animated:YES]
and when popping:
[nc popViewControllerAnimated:YES]
[nc setNavigationBarHidden:YES animated:YES]
But in my case, while I could do pushing as above, I didn't want to alter my class B and instead wanted it to not know of care that the navigation bar wasn't previously hidden (since its not my code). Also, that view gets popped using the normal Back button, there was no explicit call to popViewControllerAnimated:. What was going to work best in my code was to make my class A be the UINavigationController delegate and hide the toolbar on a delegate method call when the pop occurs.
Unfortunately I found that the UINavigationControllerDelegate methods weren't too helpful, willShowViewController & didShowViewController are called indistinguishably when pushing my class B view or when popping back to it from another one that it has pushed.
I followed a suggestion in https://stackoverflow.com/questions/642312/ about overriding UINavigationController and I made some custom delegate methods, one is called right after [super popViewControllerAnimated:]. My subclass is available at https://gist.github.com/jpmhouston/6118713 and delegate method is:
- (void)navigationController:(UINavigationController *)navigationController isPoppingViewController:(UIViewController *)poppedViewController backTo:(UIViewController *)revealedViewController {
if (revealedViewController == self && [poppedViewController isKindOfClass:[MyClassB class]]) {
[navigationController setNavigationBarHidden:YES animated:YES];
// ...and more code to run only when going from class B back to class A
}
}
I'm sure there are simpler ways to have setNavigationBarHidden: called following the Back button being pressed, but this worked for me.
In my Main Window IB file I have a TabBarController and the first controller is a Navigation Controller. When I push my detail view (after pressing a cell in a table view) I want to push my detail view and display a tool bar instead of the tab bar. The problem is that when I try
tabBar.hidden = visible;
in my detail view controller (viewDidLoad) the tabbar dissapears before the animation between the first view and the detail view is done.
What i want to achieve can be seen in the native photo app when pressing on one of the images from a gallery. There the tabbar moves out with the animation of the first view.
How do I achieve this?
Thanks in advance
check out the 'hidesBottomBarWhenPushed' property on your detail's page subclass of UIViewController
either override this method
- (BOOL)hidesBottomBarWhenPushed
{
return YES;
}
or i'm guessing this would work the same:
self.hidesBottomBarWhenPushed = YES;
as far as showing the toolbar try:
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setToolbarHidden:NO animated:YES];
}
and on the way out
- (void)viewWillDisappear:(BOOL)animated
{
[self.navigationController setToolbarHidden:YES animated:YES];
}
I added a modalView to my App, everything working fine, but on closing the modal, the whole modalView jumps about 1-2 centimeters to left while it disappears.
I did not find any reason for it yet, so here is the code regarding modal:
AppController:
- (void) showNameModal:(Player *)player
{
namesModal = [[PlayerModalView alloc] init];
namesModal.delegate = self;
namesModal.player = player;
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:namesModal];
navCon.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:navCon animated:YES];
[navCon release];
[namesModal release];
}
- (void)didDismissModalView
{
[self dismissModalViewControllerAnimated:YES];
}
ModalView:
- (void)dismissView:(id)sender
{
[delegate didDismissModalView];
}
called via navigation buttons as well ass via keyboard by
[self dismissView:nil];
As you can see, there is nothing special in it, could be taken from a manual actually.
What happens in detail:
Modal appears in center of screen, slides in from the bottom. centered all time.
i can handle some actions in the modalView, it stays centered.
now, dismissing the view makes it jumping to the left, than slides out.
Since it's a forced landscape-right app (currently), I was only able to notify the left-jump.
Any ideas how to get this jumping away?
Thanks
Try this,
- (void)didmissView:(id)sender
{
[self.navigationController didmissModelViewControllerAnimated:YES];
}
You are not modally presenting an instance of PlayerModalView but rather a UINavigationController. The left jerk you see is most likely the default animation of the navigation controller attempting a slide transform to the (non-existant) previous view.
It doesn't sound like you need a navigation controller for the PlayerModalView. Instead, you should create an ordinary view controller for it.
This solution seems to work well: Modal View Controller with keyboard on landscape iPad changes location when dismissed
To simplify resigning the first responder (if finding it is difficult), you can just call
[self.view endEditing:YES];
[self dismissModalViewControllerAnimated:YES];
The problem is that the UIViewController you're showing modally doesn't allow the orientation you're presenting it in, so when it disappears, it will do that in a direction that it considers "allowed".
Add this to the UIViewController for you modal view:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
I've been wandering how to hide / remove / disable only the main or first navigation bar in the navigation controller so that I could put an image as a whole background screen but I couldn't find any solution.
Did try hide the titleview in viewdidLoad of the main navigation controller but didn't work. Tried using navigationBarHidden but it hides the whole navigation bar for the next stack of controller.
So, I'm not sure how to do this. To give you an example, I would like to have something like this app - The Masters Golf Tournament - http://appshopper.com/sports/the-masters-golf-tournament.
If you look at Screen 1, it doesn't have any nav bar at the top but when you touch any options it will push to a new view controller and have the nav bar appear as in Screen 3,4 and 5.
Hope anyone could help me with this.Thanks a lot!
In most of my applications I have a custom UIViewController class that I derive all other custom controllers from. In some of these, I added a method like navigationBarInitiallyHidden to the base class that other classes can override. The default result depends on the nature of the application.
In the delegate of the navigation controller, when a controller is being shown that implements that method, the delegate hides or shows the navigation controller accordingly. Since I animate the hide or show, I check the current state and do nothing if no change is needed.
You could do something simpler in your delegate method. If the controller being shown is the root controller, hide the navigation bar, otherwise show it if it is hidden.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ( viewController == rootController ) {
[navigationController setNavigationBarHidden:YES animated:animated];
} else if ( [navigationController isNavigationBarHidden] ) {
[navigationController setNavigationBarHidden:NO animated:animated];
}
}
You can hide the navigation bar:
[self.navigationController setNavigationBarHidden:YES];
and where you want to show the navigation bar again
[self.navigationController setNavigationBarHidden:NO];
hide/disable
self.navigationController.navigationBarHidden = YES;
show/Enable
self.navigationController.navigationBarHidden = NO;
You can hide navigation bar by using bellow code. Below code will hide navigationbar at the time of viewWillAppear.
Objective C
-(void)viewWillAppear:(BOOL)animated
{
[[self navigationController] setNavigationBarHidden:YES animated:NO];
}
Swift
self.navigationController?.setNavigationBarHidden(true, animated: animated)
I have a view that I want to take up the full screen, so I override the init method, and some of the view methods:
- (id) init {
if (self = [super init]) {
self.wantsFullScreenLayout = YES;
}
return self;
}
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
}
- (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
}
Now, from another screen, I want to display it as a modal view:
UIViewController *screen = [[MyScreen alloc] init];
[self presentModalViewController:screen];
[screen release];
All pretty standard stuff. When I want the full-screen view to go away, however, the previous view is shifted or stretched up by about 40 pixels.
Specificially, I have a UITabBarController with a UINavigationController inside, displaying a UITableViewController, which is the view that displays the subview, and also the view that gets shifted up. If the table is not in a navigation controller, everything works just fine, nothing gets shifted up at all. If I experiment with commenting out the wantsFullScreenLayout and setStatusBarHidden lines with no navigation bar, it sometimes shifts up just 20 pixels, or doesn't actually display on the full screen (but later it does without changing any code), or sometimes doesn't break at all (but I am not getting the full full screen with any of these)
What am I doing wrong?
Through some combination of Sean's suggestion and jumping up the responder chain, I've found a solution that works is what seems like all circumstances (so far).
First issue:
The Table View by itself does not display in a navigation controller, but may show up in one if being selected from the more view in the tab bar, and that's the case where displaying the modal view in full screen causes the table to underlap the navigation bar upon return.
Second issue:
When not displayed in a navigation controller, presenting the modal view does not take up the full screen (even though wantsFullScreenLayout is set to YES). When returning from this view, the view is shifted up by 20 pixels and you can see a gap between the bottom of the table and the top of the tab bar.
Solution:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:NO];
[self.navigationController setNavigationBarHidden:NO animated:NO];
}
- (void) presentModalViewController:(UIViewController *)screen animated:(BOOL)animated {
UIResponder *responder = self;
while (responder && ![responder isKindOfClass:[UITabBarController class]]) {
responder = [responder nextResponder];
}
[(UIViewController *)responder presentModalViewController:screen animated:YES];
}
The toggling of the navigation bar's visibility forces the relayout. Overriding presentModalViewController actually calls presentModalViewController on the tab bar controller instead, which then causes it to show in the full screen. For some reason, self.tabBarController is nil when not in the more view controller, so I had to jump up the responder chain to find it.
Your UINavigationController will get called with the viewWillAppear before the modal view is dismissed. Have you tried calling [[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO]; inside the controllers that can be visible post modal dismissal. I have run into tons of problems displaying modal views on top of UINavigationControllers when bounds change. It fights any layout changes and requires lots of resetting to previous states to get it behaving nicely. It might also not hurt to call [self.navigationController setNavigationBarHidden:NO animated:NO] as well to force layout.
If this works well it might serve you to create a simple baseclass that sets these in it's viewWillAppear and then just subclass it for all non modal view controllers.
If this doesn't work you might try placing a swap view at the top level that contains the tab bar controller and then you could remove the tab bar controller with a transition when you present your modal view. Yes this isn't technically modal but would still look nice and offer the same effect. At that time since the view controller is out of the view hierarchy it shouldn't get it's layout all munged.
I think this has to do with the timing of the presentModalViewController: call. As a test you could try adding sleep(3) before you call that method. If that fixes anything, or even if it doesn't i guess I would try moving the order of things around. maybe viewDidDisappear and viewDidAppear instead of 'Will'