2

I've searched around and I can't seem to figure it out. I'm sure many people will have links for me and such, which I've most likely already looked at. But if someone could please just show me code to do the following:

I'd like to have a left arrow in my UINavigationBar as a "Back" UINavigationItem. How can I do this? Here is my current code in my UIViewController:

theBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];

UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:@selector(backButtonSelected:)];

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Options" style:UIBarButtonItemStyleBordered target:nil action:@selector(optionsButtonSelected:)];

UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Title"];
item.leftBarButtonItem = leftButton;
item.hidesBackButton = YES;
item.rightBarButtonItem = rightButton;

[self.view addSubview:theBar];
Atulkumar V. Jain
  • 5,102
  • 9
  • 44
  • 61
CodeGuy
  • 28,427
  • 76
  • 200
  • 317
  • possible duplicate of [Adding back button to navigation bar](http://stackoverflow.com/questions/2846539/adding-back-button-to-navigation-bar) – Abizern Jan 04 '11 at 17:15
  • Here is a good post : [Creating a left-arrow button (like UINavigationBar's “back” style) on a UIToolbar](http://stackoverflow.com/questions/227078/creating-a-left-arrow-button-like-uinavigationbars-back-style-on-a-uitoolba) May it be helpful ! – monjer Dec 24 '12 at 06:55

2 Answers2

2

I think this may be what you're looking for.

// in .h file
@property (nonatomic, retain) UINavigationBar *navBar;

// in .m file just below @implementation
@synthesize navBar;


// within .m method
navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];
navBar.barStyle = UIBarStyleBlack;

UINavigationItem *title = [[UINavigationItem alloc] initWithTitle:@"Nav Bar Title"];

UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] 
                       initWithTitle:@"Back" 
                               style:UIBarButtonItemStylePlain 
                              target:nil
                              action:@selector(backButtonSelected:)];

title.leftBarButtonItem = leftButton; 

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] 
         initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                              target:nil
                              action:@selector(showActionSheet:)];

title.rightBarButtonItem = rightButton;

[navBar pushNavigationItem:title animated:YES];

[self.view addSubview:navBar];
TeXcode
  • 29
  • 2
1

You should use an UINavigationController in order to pop/push UIViewControllers on your screen. The navigation controller will add an UINavigationBar to your UIViewControllers automatically so you will not need to create them as you did.

Here is a sample. I didn't looked for memory leaks. In the app delegate you'll find this method:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{        
    // Override point for customization after application launch.
    MainVC *mainVC = [[MainVC alloc] init];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mainVC];
    [self.window addSubview:navController.view];
    [self.window makeKeyAndVisible];

    return YES;
}

MainVC is a UIViewController that represents the level 1 of the hierarchy. in it i have

- (void)loadView
{
    self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    self.view.backgroundColor = [UIColor redColor];
    self.title = @"MainVC";

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"SecondLevel" style:UIBarButtonItemStyleBordered target:self action:@selector(secondLevelSelected:)];
    self.navigationItem.rightBarButtonItem = rightButton;
}

- (void) secondLevelSelected:(id)sender
{
    SecondVC *secondVC = [[SecondVC alloc]  init];
    [self.navigationController pushViewController:secondVC animated:YES];
}

SecondVC is another UIViewController that represents the second level of the hierachy. Here you will find the back button that you want.

- (void)loadView
{
    self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 240)];
    self.view.backgroundColor = [UIColor greenColor];
    self.title = @"SecondVC";

    self.navigationItem.hidesBackButton = YES;
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithTitle:@"FirstLevel" style:UIBarButtonItemStyleBordered target:self action:@selector(leftBtnSelected:)];
    self.navigationItem.leftBarButtonItem = leftBtn;
}

- (void) leftBtnSelected:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
Atulkumar V. Jain
  • 5,102
  • 9
  • 44
  • 61
Sorin Antohi
  • 6,145
  • 9
  • 45
  • 71
  • 1
    The left bar button item is going to have a normal rectangular outline. If you want the "arrow" shape of a back button you would set the `backBarButtonItem` of the previous view controller's `navigationItem`. It's the previous view controller that defines the content of the back button in a navigation controller's stack. By default that button contains the title of the controller you will go back to but you're free to change that. – Jonah Jan 04 '11 at 18:04
  • @Robert: if you want to see what Jonah is talking about just comment from self.navigationItem.hidesBackButton=YES; to self.navigationItem.leftBarButttonItem = leftBtn; you'll see the arrow shaped button with the title "MainVC" – Sorin Antohi Jan 04 '11 at 18:32
  • So there is no way to programatically add a left arrow as an item to a bar, so that I don't have to use a UINavigationController? – CodeGuy Jan 04 '11 at 20:48
  • @Robert you can create a custom button that will have an image with an arrow and add it to the bar but in order to have the push/pop behavior you will need the navigation controller. – Sorin Antohi Jan 05 '11 at 01:25