0

i have a tabbed application and some tabs use a navigation stack. Thats fine, But the tabs that don't, i want to add a consistant layout.

So I'm wondering how to add barButtonItems to the navigation bar, without navigation stack.

So this wont work (because the navigation stack does not exist) self.navigationItem.rightBarButtonItem = customItem;

how can i add custom item to UInavigation bar?

cream-corn
  • 1,820
  • 14
  • 26

3 Answers3

1

Use interface builder to add a navigation bar to your viewController xib and add bar button item from interface builder.

Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256
  • Im trying not to use IB for this, mainly because all i have is a table view and a navigation bar, but that may be the only option. – cream-corn Jun 27 '11 at 06:19
  • 1
    If you have UINavigationBar binded through IB or code you can add bar button item to the instance of that navigation bar. Just do not use self.navigationItem.rightBarButtonItem = customItem; because as you said there is no navigation stack so just bind to the top navigaition bar you have in your view. – Rahul Vyas Jun 27 '11 at 06:26
1

If you don't care for the navigation aspect of the bar at all, then perhaps you can use UIToolbar.

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    UIToolbar *bar2 = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    [self.view addSubview:bar2];
    [bar2 release];
}
Ethan Mick
  • 9,517
  • 14
  • 58
  • 74
  • This is a valid option, and i initially tried this also, But there is a black border on the bottom of the Navigation bar. Which is on the top of the toolbar. Is there a way to fix that? – cream-corn Jun 27 '11 at 06:27
  • 1
    This is because the UIToolbar is meant to be on the bottom, hence the black line is at the top (opposite for UINavigationBar). You can take a look at this post and implement a custom background for your UIToolBar (Second one down, or the selected answer). Just Get a 1 pixel wide image of the UINavigationBar and put that for the backround of your toolbar. http://stackoverflow.com/questions/1941103/can-i-give-a-uitoolbar-a-custom-background-in-my-iphone-app – Ethan Mick Jun 27 '11 at 15:34
0

Use below to add custom item to UINavigationbar in your UIViewController inherited class.

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"my Button" style:UIBarButtonItemStylePlain target:self action:@selector(ClickButton:)];

EDITED:

For the controller which doesn't use navigation stack, you could consider using the UIToolbar for them.

Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76