Search code examples
iphoneiosxcodeuinavigationcontrolleruibarbuttonitem

How to show back button on the RootViewController of the UINavigationController?


Here is my code. I want to put a back button on the opening rootviewController.

- (void)selectSurah:(id)sender {

    SurahTableViewController * surahTableViewController = [[SurahTableViewController alloc] initWithNibName:@"SurahTableViewController" bundle:nil];
    surahTableViewController.navigationItem.title=@"Surah";

    surahTableViewController.navigationItem.backBarButtonItem.title=@"Back";

    UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:surahTableViewController];

    [self presentModalViewController:aNavigationController animated:YES];   
}

Solution

  • I don't believe it's possible to pop the root view controller off the navigation stack, but you can fake it with a UIButton added as the custom view of a UIBarButtonItem:

    UIButton *b = [[UIButton alloc]initWithButtonType:UIButtonTypeCustom];
    [b setImage:[UIImage imageNamed:@"BackImage.png"] forState:UIControlStateNormal];
    [b addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    self.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:b];
    

    A suitable PSD of iOS UI elements can be found here.