How to stop UINavigationBar default back button animation using storyboard? - ios

I am using storyboard in my app where I have to navigate to another view controller without animation.I am successfully doing it using the custom segue.But I when I come back to the previous view controller then by using navigation bar default back button then it is animating while coming backward.So going forward it is not animating and going backward it is animating.I want to stop the default animation of the back button.
Custom Seague
// PushNoAnimationSegue.h
#interface PushNoAnimationSegue : UIStoryboardSegue
#end
// PushNoAnimationSegue.m
#implementation PushNoAnimationSegue
-(void) perform{
[[[self sourceViewController] navigationController] pushViewController:[self destinationViewController] animated:NO];
}
#end
Using this code I am successfully going to the next view controller without animation since -(void) perform is called automatically in the forward direction.When I come back it is not called.So please suggest me if there is anything wrong with the implementation or I should go with some other method.

hope this will help
1.custom button on navigation bar
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(x,y,width,height)];
[btn setImage:[UIImage imageNamed:#"some_image.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(popViewController) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.leftBarButtonItem =backButtonItem;
and then
void popViewController()
{
[self.navigationController popViewControllerAnimated:NO];
}

If you use NavigationController then use this
[self.navigationController popViewControllerAnimated:NO];
Or
[self dismissViewControllerAnimated:NO completion:nil];

Related

Back button without using navigation controller in IOS

I haven't embedd navigation controller with my view controller. Now i'm using a code to go back to home screen to with the use of back button but there nothing appears, i don't know why. I don't want to use navigation controller in my vc. This is the code,
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"car_marker.png"]
style:UIBarButtonItemStylePlain
target:self.navigationController
action:#selector(popViewControllerAnimated:)];
self.navigationItem.leftBarButtonItem = backButton;
It should look like this only,
You will need to change your approach because you cannot pop without a navigation controller.
You should embed your viewcontroller into navigation controller and then you can hide with [[self navigationController] setNavigationBarHidden:YES animated:YES]; and then you can add any kind of custom button to perform pop action.
For adding a custom back button to your view
- (void)addCustomBackButtonToView
{
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(20, 20, 44.0f, 30.0f)];
[backButton setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(popVC) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backButton];
}
- (void) popVC{
[self.navigationController popViewControllerAnimated:YES];
}
You can hide your navigation controller by below code self.navigationController.navigationBar.isHidden = true
and use swipegesture option to move back, even though you have to use navigation controller,#iPeter has a valid point.
1) Add two ViewControllers make first as initial controller(entry point).
2) Add view on both ViewController with size you wish to have.
3) Add buttons on both views as subviews.
4) create segue from firstButton ->(use show) -> second controller.
5) From second ->(use show) -> first controller.
see the image below-:
This is not preferred way of doing pop/push. You must use Navigation Controller(that works like a stack for controllers you push) that will provide you default Back button or you can create custom .
You can not push or pop view controllers as you are not using the Navigation controller. Try using Present & dismiss controller which can called from any controller Instace.
Step 1 : Present to " Controller which is shown with no navigation bar but back button"
[self presentViewController:#"<ViewControllerToBePresented>" animated:NO completion:nil];
Step 2: add backBatton in ViewDidLoad
-(void)addBack{
UIButton *backBtn = [[UIButton alloc] initWithFrame: CGRectMake(20.0f, 20.0f, 50.0f, 30.0f)];
[backBtn setTitle:#"back" forState:UIControlStateNormal];
[backBtn addTarget:self action:#selector(dismissViewController) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backBtn];
}
Step 3: Dismisscontroller to go back to previous scene
-(void) dismissViewController{
[self dismissViewControllerAnimated:NO completion:nil];
}

incomplete backButton function objective C

I'm currently trying to create a popover menu, with a navigationBar, and a backButton that bring me back to the main ViewController.
Posts over here helped me a lot to create my functions.
I put NSLogs in my code and everything works perfectly.... But the backButton itself, which do nothing.
So. My back button is declared in my .h file like that:
#property (strong, nonatomic) IBOutlet UIButton *backButton;
and I have those two functions in my .m :
-(void)setBackButton:(UIButton *)backButton
{
NSLog(#"setBackButton: called");
[_backButton addTarget:self action:#selector(backBtn:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
[self.navigationItem setBackBarButtonItem:backButtonItem];
}
- (IBAction)backBtn:(id)sender {
NSLog(#"backBtn: called");
[[self navigationController] popViewControllerAnimated:YES];
}
And when I click my backButton, the NSLog is correctly displayed.
But I have no idea of what to call to go back to my view. Does anyone have an idea ??
Suppose you have two controllers A and B
So if you are adding
[[self navigationController] popViewControllerAnimated:YES];
in controller B, it will only work only when you are pushed to B using proper navigation method from A.
In case of storyboard, you have to push from A to B is like
A *aCntrl = [self.storyboard instantiateViewControllerWithIdentifier:#"A"];
[self.navigationController pushViewController:aCntrl animated:YES];
In case of non-storyboard, you have to push from A to B is like
A *aCntrl = [[A alloc] initWithNibName:#"A" bundle:nil];
[self.navigationController pushViewController:aCntrl animated:YES];
In this case
[self.navigationController popViewControllerAnimated:YES];
should work.
Just check how you are navigation from A to B. It should be pushViewController and not presentViewController.

hidesBackButton works but backbarbuttonitem setAction not working

I'd like to change action of my backbutton in one of my ViewController. Instead of going back to the precedent view, I want to perform an action in the same ViewController.
self.navigationItem.hidesBackButton=NO;
hides the BackButton but
[self.navigationController.navigationItem.backBarButtonItem setAction:#selector(performBackNav:)];
and
[self.navigationItem.backBarButtonItem setAction:#selector(performBackNav:)];
do the same as before (going back to precedent ViewController). Nothing changes.
-(void)performBackNav:(id)sender {
//Actions
[self.navigationController popViewControllerAnimated:NO];
}
Any ideas to modify backbarbuttonitem's action ?
Instead of overriding the action method of default back button create a custom button. this is one of the way Try this
{
UIButton *urButton = [UIButton buttonWithType:UIButtonTypeCustom];
urButton.frame = urRequiredFrame;
[urButton setImage:urImage forState:UIControlStateNormal];
[urButton addTarget:self action:#selector(performBackNav:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *doneButton =[[UIBarButtonItem alloc] initWithCustomView:urButton];
self.navigationItem.leftBarButtonItem=doneButton;
}
-(void)performBackNav:(id)sender {
//Actions
[self.navigationController popViewControllerAnimated:NO];
}

Back button not responding ?

I am newbie in iOS development and currently stuck on creating back button. I already placed popViewControllerAnimated method on tapping button, also I test it on debugging mode and back button method is calling but popViewControllerAnimated is not working.
Here is the code.
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.height-100), 30, 80, 20)];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(BackButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backButton];
//some piece of code...
}//end of viewDidLoad method
-(void)BackButtonClicked: (id)sender{
[self.navigationController popViewControllerAnimated:YES];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"testing" message:#"some message" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:#"NO", nil];
[alert show];
}
On tapping back button this alert is showing but screen is not going back.
Any help regarding this issue will be highly appreciable.
I called this viewController by writing this code in my previous viewController
PlaceTranslatedDetailsViewController *secondViewController =
[self.storyboard instantiateViewControllerWithIdentifier:#"placeTranslatedDetail"];
[self presentViewController:secondViewController animated:YES completion:NULL];
However, this viewController and previous viewController were designed programmatically
If your viewcontroller is not an rootviewcontroller for a uinavigationcontroller than the method popViewControllerAnimated won't work. So you need to have a uinavigationcontroller behind in order to push/pop a viewcontroller.
So in your case you have to use [self dismissViewControllerAnimated:YES completion:nil]; instead of popViewController because you presented modally a view controller.
You are not presenting your view controller as part of a navigation stack, so using popViewControlleranimated: is not going to work.
You need to use
[self dismissViewControllerAnimated:YES completion:NULL];
instead.
use this code:
[self dismissViewControllerAnimated:YES completion:NULL]
A push (via pushViewController or storyboard push segue) will need a call to popViewController
A presented view (via presentVewController or a storyboard modal segue) needs a dismissViewController call.

How to call some code if users press back button

In UINavigationController?
For example, say I want to make sure that the UINavigationController is not animating when user press the back button.
If you aren't intending to intercept the back button tap itself but instead the act of the current view controller disappearing, you can use:
- (void)viewWillDisappear:(BOOL)animated {
if (self.isMovingFromParentViewController) {
// handle back button press
}
}
If you are sure you want to do the back button, you can create your own custom UIBarButtonItem and set it to the current controller's leftBarButtonItem. Be sure to call [self.navigationController popViewControllerAnimated:YES] after you have finished doing your own logic.
Add below code in ViewDidLoad of your application :
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[btnBack setFrame:CGRectMake(0.0f, 0.0f, 55.0f, 35.0f)];
[btnBack addTarget:self action:#selector(backClicked:) forControlEvents:UIControlEventTouchUpInside];
[btnBack setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
self.navigationItem.leftBarButtonItem = backButton;
This create a custom Back button. "Back.png" is image same as that of OS.
Add below code as function. This one pops to RootViewcontroller.
- (void) backClicked:(id)sender {
// perform certain task
// If task is completed then call below LOC
[self.navigationController popToRootViewControllerAnimated:YES]; }

Resources