3

I am using the following code to achieve a navigation bar in my app. (my app was crashing when I used a push segue so I need a modal segue meaning the nav bar is hidden after the modal segue is called)

UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
//do something like background color, title, etc you self
[self.view addSubview:navbar];

Does any one know any methods I can use with the above code to achieve back button functionality in the nav bar??

thisguy
  • 51
  • 2
  • 8
  • If you want a back button within a nav bar then why don't you push the viewcontroller instead of modally presenting it? – Raja Vikram Jul 03 '15 at 04:27
  • good question: its because i had to change the segue type from a push to modal because it was crashing my app for some reason whenever i would tap the image in the view controller that would call the segue. I found this weird too because it worked fine for a long while... – thisguy Jul 03 '15 at 04:30
  • Then try to correct that error and use push segue. Then you wont have to use unnecessary code. – Raja Vikram Jul 03 '15 at 04:36
  • I tried but was getting a Thread 1: EXC_BAD_ACCESS error as the app crashed whenever I triggered the segue. The compiler gave me no real clue how to fix it. Changing the segue type to modal was the only fix to not get a crash. I am still not sure why the app crashes when using a push segue. – thisguy Jul 03 '15 at 04:46
  • possible duplicate of [Adding back button to navigation bar](http://stackoverflow.com/questions/2846539/adding-back-button-to-navigation-bar) – Anbu.Karthik Jul 03 '15 at 05:26
  • I saw that question before asking @anbu.karthik.i still don't know which code to use IN ADDITION my code above... – thisguy Jul 03 '15 at 05:30

5 Answers5

4

Use below code to add back button on left side of navigation bar.Add UIBarButtonItem on Navigation bar.

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] 
                           initWithTitle:@"Back"                                            
                           style:UIBarButtonItemStyleBordered 
                           target:self 
                           action:@selector(backBtnClicked:)];
self.navigationItem.leftBarButtonItem = backButton;
poojathorat
  • 1,200
  • 2
  • 9
  • 19
4

Use below code to add back button on left side

   UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
//do something like background color, title, etc you self
[self.view addSubview:navbar];

UINavigationItem *item = [[UINavigationItem alloc]
                               init];
navbar.items= @[item];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                               initWithTitle:@"Back"
                               style:UIBarButtonItemStyleBordered
                               target:self
                               action:@selector(backBtnClicked:)];
item.leftBarButtonItem = backButton;
Gaurav Patel
  • 957
  • 1
  • 6
  • 16
  • I get the warning 'property leftbarbuttonitem not found on object of type UINavigationbar' with the above code. I also tried `self.navigationItem.leftBarButtonItem = backButton;` but still nothing. this is weird...? – thisguy Jul 03 '15 at 06:37
  • Ok, Finally shows a back button but tapping it crashes the app? whats going on...? any ideas?this is what the debugger area is saying `Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[StreamPhotoScreen backBtnClicked:]: unrecognized selector sent to instance 0x16fd66d0'` – thisguy Jul 03 '15 at 07:20
  • should we programmatically set a performSegueWithIdentifier code block to stop the crash? – thisguy Jul 03 '15 at 07:51
  • define method in your class -(IBAction)backBtnClicked:(id)sender – Gaurav Patel Jul 03 '15 at 07:56
  • in .h? am i to connect it to an outlet? – thisguy Jul 03 '15 at 08:43
2

BackButton is better than LeftButton I think:

UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] init];
[desVC.navigationItem setBackBarButtonItem:backBtn];
Allen
  • 6,745
  • 5
  • 41
  • 59
1

Add Back Button With Image :

UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 70.0f, 21.0f)];
UIImage *backImage = [UIImage imageNamed:@"backBtn"];
[backButton setImage:backImage  forState:UIControlStateNormal];
[backButton setTitleEdgeInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 0.0)];
[backButton setTitle:@"Back" forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[negativeSpacer setWidth:-15];

self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,backButtonItem,nil];
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
1

Regarding the back button, the proper way is not to create your own.

The UINavigationItem class has proper support for this:

  • func setHidesBackButton(_:animated:)
  • var leftItemsSupplementBackButton: Bool

So, if you want to add a custom button on the left side, just set leftItemsSupplementBackButton to true.

Or call

self.navigationItem.setHidesBackButton(false, animated:false)