I've tried various ways of hiding the back button of my UINavigationController
UINavigationController RVC = new UINavigationController();
other code
RVC.NavigationItem.SetHidesBackButton(true,true);
and other similar ways, but none of them have actually hidden the back button. Does anyone know what I'm doing wrong?
Use this.NavigationItem.SetHidesBackButton(true,true); within the controller that it is pushed in the navigation controller. You could override ViewWillAppear and put the code there like:
public override void ViewWillAppear (bool animated)
{
base.ViewWillAppear (animated);
this.NavigationItem.SetHidesBackButton(true,true);
}
Hope it helps.
A simple note
Since the navigation bar is unique for a UINavigationController, the button will maintains its state for all the controllers you push in the navigation controller. To explain the concept suppose you have two controllers, say A and B. You first push A and in its ViewWillAppear method you hide the button. When you push B, the button still remains not visible. If you want to unhide the button in B, you can play with its ViewWillAppear method (like before) and so on...
Related
I don't want to let the navigation controller show its navigation bar in the whole project.
Now there are three view controllers
(1) Login view controller
(2) Sign up view controller
(3) Home view controller.
I just hope to use action(which can be triggered by any kind of event, i.e. drag gesture, not necessary the pressing button) to switch between these view controllers. But I found once I get to the "signup view controllers", I can not go back to the login view controller, since there is no "BACK" navigation bar.
Questions:
How "PUSH" in one view controller, then "POP" in the other view controller?
Or there is some different way to solve this problem?
Thank you so much, any suggestion is great.
To programmaticaly go backward in a navigation controller's navigation stack, call this method:
[self popViewControllerAnimated:YES];
When and where you call this is up to how you want your app to flow. Essentially, the default navigation controller calls this automatically when the navbar's back button is pressed. But if you hide the navbar and still need to pop back, you can call this method to pop back.
As for pushing forward, it's simply a matter of creating a Push segue on the storyboard, giving it a name, and then in your code, call this method:
[self performSegueWithIdentifier:#"segue_YOUR_SEGUE_ID" sender:self];
On the question of your app, what probably makes most sense is for the login view be a view by itself. It should contain a modal segue to a sign up view for new users as well as a modal segue to the home view controller (which may or may not need to be embedded in a navigation controller).
Performing a modal segue works exactly the same as a push segue (if you're using storyboards. Hook up the segue, choose a modal segue, then call the performSegueWithIdentifier: method in your code when you need the segue to occur.
Dismissing a modal view is slightly different, but still quite simple. It goes like this:
[self dismissViewControllerAnimated:YES completion:nil];
It's fairly check to do with an 'if' statement...
if (self.navigationController.navigationBarHidden == NO) {
//YOUR ACTION
}
Hope that helps!
I have a Storyboard with three views A, B and C (and more). From view B, the user can go back to view A by tapping the left button bar item that's automatically created. I've changed the label on this to "Cancel" by setting the Back Button property of A's navigation item.
C should have the same Cancel button, but it doesn't make sense to go back to view B; rather it should jump back to A. I know how to do this programatically but where do I put the code so it's triggered when C's Cancel button is tapped?
I found this way simplest, just put index:
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
I think you cannot override the back button function. What I do in those cases, is create a left bar item and on it's function decide which navigationController view to send the user to.
I do that by using:
[self.navigationController viewControllers] objectAtIndex:1];
Index 1 will be the first view after the root viewController, if A is your rootView, you can also use:
[self.navigationController popToRootViewControllerAnimated:YES];
If you want to cancel an action from B or C and go back to A it may make more sense to present the view controllers modally.
From the iOS human interface guidelines:
A modal view generally displays a button that completes the task and dismisses the view, and a Cancel button users can tap to abandon the task.
Use a modal view when you need to offer the ability to accomplish a self-contained task related to your application’s primary function. A modal view is especially appropriate for a multistep subtask that requires UI elements that don’t belong in the main application user interface all the time.
Instead of pushing C, use the replace option in the segue so that it will do this automatically for you.
Another option: if you put the following into B, it will remove it from the stack when C is presented. Then the stack looks like [A,C] so back will go straight to A.
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
// remove this view controller from the stack
if let nav = self.navigationController {
let vcs = nav.viewControllers.filter {(vc) in
return (vc as? MyViewController) == nil
}
self.navigationController?.setViewControllers(vcs, animated: false)
}
}
My UIViewController calls a function on my rootViewController which then called popToRootViewControllerAnimated to return the view to the rootController. This all works - great!
Unfortunately the UINavigationItem (toolbar at the top) seems to display a mashup of both the rootViewController and the UIViewController that has just been removed.
What do I need to do? What have I done wrong?
The navigation bar doesn't remember changes that were made to it, so when you push a new controller, the navigation bar is altered to give the title of the new view controller, but it doesn't store what was there for the previous view controller.
You will need to recreate the items in the toolbar each time you come back to the view controller that has custom items.
You might be able to do this on viewWillAppear instead of viewDidLoad. I can't recall exactly, but you should recreate custom controls on navigation toolbar because it does not get preserved when a new view controller is pushed.
It seems that calling popToRootViewController from the rootViewController messes things up. TO rectify this I called the following from within the calling UIViewController
[self.navigationController popViewControllerAnimated:YES];
I have a a UIViewController that is pushed to by two different views in my app.
One time it is a modal view, so I have the right navbar button set to Done and it dismisses the view.
At another time in my app, this same view is pushed to, but not modally, thus I don't want this button to show. I tried adding this when pushing it, but no luck.
self.navigationItem.rightBarButtonItem.enabled = NO;
You can check the parent view controller for whether it has the modalViewController property set
if (self.parentViewController.modalViewController == self)
{
// add button
}
Simple and effective -
self.navigationItem.rightBarButtonItem = nil;
Edit:
How can you add this when you are pushing this ? Add it in the viewWillAppear or viewDidLoad of the viewController you want to see this is in.
You can check for a certain condition.
If it is pushed from view 1, you can make it nil.
If it is shown modally from view 2, you can make it appear.
For this, you will have to make the viewControllers communicate with each other. For that, you will need to use NSUserDefaults and set an integer for a key.
You can assign two different integers logically and use them as the condition for showing/not showing the rightBarButtonItem.
Good Luck.
I have a UINavigationController in which I push / display several other UIViewControllers. I would like a specific title and button (a Cancel button to be precise, which appears on the right-hand side) to always appear in the UINavigationController's UINavigationBar regardless of the view controller being displayed within the navigation controller. At the moment I have set the .title and .navigationItem.rightBarButtonItem of each individual view controller to be the same, but this seems like (a) a lot of wasted effort; and (b) when a new view controller is pushed onto the stack there is a brief animation where the title and button can be seen animating out and then back in - obviously not desirable since I want to give the appearance that those items are static and never change.
Any suggestions would be greatly appreciated.
Try to set all you need in UINavigationController and then in viewWillAppear of your UIViewControllers subclasses paste:
[self.navigationItem setLeftBarButtonItem:self.navigationController.navigationItem.leftBarButtonItem];
[self.navigationItem setRightBarButtonItem:self.navigationController.navigationItem.rightBarButtonItem];