My question is simple, How to hide back button in navigation bar? I see similar question here in stack overflow but
self.navigationItem.hidesBackButton = YES;
not working for me. I am using below code because RootViewController is my Singleton class.
Thanks
i still use both in viewDidLoad but both are not working
[RootViewController sharedFirstViewController].navigationItem.hidesBackButton = YES;
[RootViewController sharedFirstViewController].navigationItem.backBarButtonItem=nil;
Try self.navigationItem.backBarButtonItem.hidden = YES; or self.navigationItem.backBarButtonItem = nil;
Place one of these either in viewWillAppear:, viewWillLoad or viewDidAppear: of the class you want to get rid of the back button in.
I suggest doing this before the view appears on the screen. You probably dont want to see the bar and then have it disappear.
So you should call:
- (void) viewDidLoad {
//Check to see if the Nav har is hidden, and then hide it
if (!self.navigationItem.backBarButtonItem.hidden) {
self.navigationItem.backBarButtonItem.hidden = YES;
}
else {
NSLog(#"back button already hidden");
}
}
Note that you can also do this with the whole nav bar completely, if you're trying to free up screen space.
Related
I've Tab bar application and i will load the other view controller by navigation controller when i come back the tab bar is hiding i searched and i used
for(UIView *view in self.window.subviews)
{
if([view isKindOfClass:[UITabBarItem class]])
{
if(view.hidden){
view.hidden = NO;
break;
}
view.hidden = YES;
}
}
but the problem remains same can any one know the solution?
Thanks in advance
I am not sure what kind of result you want to achieve but perhaps you can check previous question to hide and display TabBar
How to hide uitabbarcontroller
If it doesn't work you can try to paste the code you are using to declare the TabBar
I have a button when it pressed, I want it to take me to another view (the "news" view). Within the news view, I want there to be a navigation bar with a back button. I have a navigationcontroller setup throughout my app but I can't seem to get this to work when this button is pressed. It takes me to the view I want but there is no navigation bar and no back button. This is my code that is implemented when the button is pressed.
If anybody know what I am doing wrong, it would be much appreciated.
Thanks
-(IBAction)news
{
newsViewController *view1 = [[newsViewController alloc] initWithNibName:#"newsViewController" bundle:nil];
view1.title=#"news";
[self.navigationController pushViewController:view1 animated:YES];
}
I am not in my mac, so I can not test code, but if it is working and the only issue you got is not show the bar, what you need to is set the bar to be visible:
From apple docs:
The navigation toolbar is hidden by default but you can show it for
your navigation interface by calling the setToolbarHidden:animated:
method of your navigation controller object. If not all of your view
controllers support toolbar items, your delegate object can call this
method to toggle the visibility of the toolbar during subsequent push
and pop operations.
Something like that is supposed to work:
-(IBAction)news {
newsViewController *view1 = [[newsViewController alloc] initWithNibName: #"newsViewController" bundle:nil];
view1.title=#"news";
[self.navigationController pushViewController:view1 animated:YES];
//Add this line!
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
I hope it can help you.
write the below code in page where you want to show navigation controller
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = NO;
}
When a view loads, i want to see if it's because the user pressed the back button. How can i check this?
The best solution I've found to detect a UINavigationController's back button press (pre-iOS 5.0) is by verifying that the current view controller is not present in the in the navigation controller's view controller stack.
It is possibly safer to check this condition in - (void)viewDidDisappear:(BOOL)animated as logically, by the time that method is called it would be extremely likely that the view controller was removed from the stack.
Pre-iOS 5.0:
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if (![[self.navigationController viewControllers] containsObject:self]) {
// We were removed from the navigation controller's view controller stack
// thus, we can infer that the back button was pressed
}
}
iOS 5.0+ you can use -didMoveToParentViewController:
- (void)didMoveToParentViewController:(UIViewController *)parent
{
// parent is nil if this view controller was removed
}
in your viewWillDisappear method check
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if ([self isMovingFromParentViewController]) {
//specific stuff for being popped off stack
}
}
This is only for post iOS 5
UINavigationController has a delegate property that issues delegate callbacks. Please see the iOS reference here.
The delegate doesn't have a "back button pressed" callback, but instead it tells you when something is going to appear on the navigation stack. When you press back, you are "popping" the top view controller off the stack, so it will tell you that the view is about to appear. I think this is the callback you'd be looking for.
You could have some simple logic to check if it's the view controller that's "interested", and then you could send a notification, et al.
For the sake of completeness, mix of two most upvoted answers (1, 2) in Swift:
override func willMoveToParentViewController(parent: UIViewController?) {
super.willMoveToParentViewController(parent)
if parent == nil {
// view controller is popping
}
}
This is a slightly different scenario, but I thought the solution might help others out.
In my situation, I had a UINavigationController within a UIPopoverController. I needed to detect whether the user clicked the back button, or clicked outside of the popover. To do this I checked the visibleViewController property in viewWillDisappear. If the view controller is still the visibleViewController when closing, then the popover is being closed by another means. If the view controller is not the visibleViewController when closing, then the back button was pressed.
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (self.navigationController.visibleViewController != self) {
<Do something since we're closing using something else>
} else {
<Do something since we're closing because of the back button>
}
}
I tried using zach's solution, but isMovingFromParentViewController returns true for both cases.
I verified this works in iOS 5+
I hope this helps.
Create a custom back bar button and set the target,
Step 1: Add these methods to your class
- (void)backButtonClicked :(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)addBackBarButton{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 55, 35);
[button setTitle:#"back" forState:UIControlStateNormal];
[button addTarget:self action:#selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
}
Step 2: Call [self addBackBarButton]; in viewDiDLoad method
You will get the action in backButtonClicked method. You can play around with it the way you want.
Cheers!
The only way to do this so you know for sure that it was the back button is to create a custom button. If you don't know how to do that, check out this tutorial. It won't look exactly like the normal back button, but close. If you need more help, post a comment
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 am having some trouble with view hierarchies and drawing on the iPhone.
To be more specific, I have a tab bar application with a certain tab that contains a table view where I would like the selection of a specific cell to have a UIPickerView slide up. The sliding isn't really a problem (or at least I'm assuming it won't be once I figure this part out), but I cannot seem to get the picker (or any UIView, for that matter) to show up over the tab bar.
I think the way I have this certain tab set up may be the problem. Normally, in any other tab, I could just do something like this:
[self.tabBarController.view addSubview:pickerView];
and everything would work fine.
But for this specific tab, I have a UISegmentedControl in the navigation bar that switches between two different UITableViews. So the view controller associated with the tab (call it TabViewController) has its own instances of these two table view controllers (TableOneViewController and TableTwoViewController) and will insert the currently selected table view (based on the segmented control) as a subview of TabViewController's view.
If I didn't need to switch the views like this I could just call
[tabViewController.tabBarController.view addSubview:pickerView];
from TabViewController and the picker would show up over the tab bar. But the thing is I cannot call this in either of the table view controllers selected within this tab (well I can, but it doesn't do anything). I have tried passing this tabBarController property into the table view controller, but that doesn't work either. I have also tried messing around with the app delegate (which I'm trying to avoid) to no avail.
Is there something simple I'm missing here, or can this not be done? I feel like it should since a keyboard can slide up over the tab bar in this table view. Is there a way to just draw over all the current views and subviews?
Here is what is called when the segmented control is selected within TabViewController.m, and switches the views:
- (IBAction)switchViews:(id)sender
{
if (self.tableOneViewController.view.superview == nil)
{
if (self.tableOneViewController == nil)
{
TableOneViewController *tempOneController = [[TableOneViewController alloc] initWithNibName:#"TableOneViewController" bundle:nil];
self.tableOneViewController = tempOneController;
[tempOneController release];
}
[tableTwoViewController.view removeFromSuperview];
[self.view insertSubview:tableOneViewController.view atIndex:0];
}
else
{
if (self.tableTwoViewController == nil)
{
TableTwoViewController * tempOneController = [[TableTwoViewController alloc] initWithNibName:#"TableTwoViewController" bundle:nil];
self.tableTwoViewController = tempOneController;
[tempOneController release];
}
[tableOneViewController.view removeFromSuperview];
[self.view insertSubview:tableTwoViewController.view atIndex:0];
}
}
And here's what's going on when I try to add the picker in TableOneViewController.m:
UIPickerView *tempPicker = [[UIPickerView alloc] init];
tempPicker.delegate = self;
tempPicker.dataSource = self;
tempPicker.showsSelectionIndicator = YES;
tempPicker.clipsToBounds = NO; // thought this might work, but doesn't
self.picker = tempPicker;
[tempPicker release];
[self.view addSubview:pickerPicker]; // adds beneath tab bar!
[[[UIApplication sharedApplication] keyWindow] addSubview:someViewController];
Make sure the viewController you are loading in is 480px high!
To remove it again, use [someViewController removeFromSuperview];
Maybe you want to do just
[tabBarController presentModalViewController:someViewController animated:YES];
?
This should slide on top of anything else you have on the screen.