How to add a right bar button to a navigation bar in iphone

75,950

Solution 1

-(void)viewDidLoad
{ 
    [super viewDidLoad];
    app.navigationController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(Add:)] autorelease];
}

-(IBAction)Add:(id)sender
{
    TAddNewJourney *j=[[TAddNewJourney alloc]init];
    [app.navigationController pushViewController:j animated:YES];
    [j release];
}

Try the other answers. I posted this answer so that it will work if your viewcontroller does not have a navigation controller which i think is the problem.

Solution 2

Let assume, you have a UIViewController, like this:

UIViewController *vc = [UIViewController new];

And you added it to navigation controller:

UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];

In this way rightBarButtonItem will NOT getting displayed:

nc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"xyz" style:UIBarButtonItemStyleDone target:self action:@selector(xyz)];

BUT this way IT WILL appear:

vc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"xyz" style:UIBarButtonItemStyleDone target:self action:@selector(xyz)];

Use your own viewcontroller instead of the navigationcontroller to refer navigationItem.

Solution 3

Add this code in viewdidload

UIBarButtonItem *chkmanuaaly = [[UIBarButtonItem alloc]initWithTitle:@"Calculate" style:UIBarButtonItemStylePlain target:self action:@selector(nextview:)];

self.navigationItem.rightBarButtonItem = chkmanuaaly;

Solution 4

Most of the answers here address setting the UINavigationBar's rightBarButtonItem from within the new VC after it is shown. My need, which was actually answered by János, is to set the UINavigationItem items from the CALLING UIViewController. Hence, the following code (Thanks, János).

// from within self (aka the calling controller):

    UIViewController *c = [[[UIViewController alloc] init...] autorelease];
    c.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissModalViewControllerAnimated:)] autorelease];
    c.navigationItem.title = @"Title Goes Here";
    UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:c] autorelease];
    nav.navigationBarHidden = FALSE;
    [self presentModalViewController:nav animated:YES];

Now, granted, this may seem redundant to János' answer, but I need more rep points in order o mark his response up. ;-)

Solution 5

I guess it will be more complete response and it should help in any situation:

-(void)viewDidLoad{

    //Set Navigation Bar
    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];

    //Set title if needed
    UINavigationItem * navTitle = [[UINavigationItem alloc] init];
    navTitle.title = @"Title";

    //Here you create info button and customize it
    UIButton * tempButton = [UIButton buttonWithType:UIButtonTypeInfoLight];

    //Add selector to info button
    [tempButton addTarget:self action:@selector(infoButtonClicked) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem * infoButton = [[UIBarButtonItem alloc] initWithCustomView:tempButton];

    //In this case your button will be on the right side
    navTitle.rightBarButtonItem = infoButton;

    //Add NavigationBar on main view
    navBar.items = @[navTitle];
    [self.view addSubview:navBar];

}
Share:
75,950

Related videos on Youtube

Rani
Author by

Rani

Updated on July 09, 2022

Comments

  • Rani
    Rani almost 2 years

    I want to add a right bar button item to the navigation bar, so that on click, it performs certain a function.

    I have created the following code to add the right bar button item, but after it is done, the bar button item is not getting displayed in navigation bar:

    -(void)viewDidload{
        self.navigationItem.rightBarButtonItem = 
        [[[UIBarButtonItem alloc] 
               initWithBarButtonSystemItem:UIBarButtonSystemItemAdd                                                                                                     
                                    target:self
                                    action:@selector(Add:)] autorelease];    
    }
    
    -(IBAction)Add:(id)sender
    {
        TAddNewJourney *j=[[TAddNewJourney alloc]init];
        [app.navigationController pushViewController:j animated:YES];
        [j release];
    }
    
  • Rani
    Rani about 13 years
    i have added this code but my right bar button item is not visible in navigation bar
  • Rani
    Rani about 13 years
    i have tried this code but my right button item is not visible in navigation bar
  • Rani
    Rani about 13 years
    i have tried this code but my right button item is not visible in navigation bar
  • visakh7
    visakh7 about 13 years
    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(Add:)]; app.navigationController.navigationItem.rightBarButtonItem=b‌​utton; [button release]; try this
  • Aman Aggarwal
    Aman Aggarwal about 13 years
    i think the navigation bar is not a part of navigation controller.Do you put the navigation bar in xib file of view controller?.If then add the button also in .xib file. Hope it helps you
  • chris838
    chris838 about 10 years
    Yes this change was exactly what I needed.
  • Narendra Baratam
    Narendra Baratam over 7 years
    if you're calling a viewcontroller by programmitically by navigationcontroller then [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addBtnPressed)]];
  • Larry Lo
    Larry Lo over 7 years
    Oops. Should I map my view controller to navigation view controller in Storyboard ? I am using Xcode 8.2 , iOs 10
  • Ucdemir
    Ucdemir about 2 years
    rightbar button is not seen using this code