Adding buttons to navigation bar

20,000

You can use the UISegmentedControl. Check the UICatalog code sample to check its usage in the navigation bar.

Here is some sample code:

       - (void)viewDidLoad {
[super viewDidLoad];

 UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
                          [NSArray arrayWithObjects:
                           [UIImage imageNamed:@"up.png"],
                           [UIImage imageNamed:@"down.png"],
                           nil]];
      [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
      segmentedControl.frame = CGRectMake(0, 0, 90, 35);
      segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
      segmentedControl.momentary = YES;

      UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
        [segmentedControl release];

      self.navigationItem.rightBarButtonItem = segmentBarItem;
        [segmentBarItem release];
}

    - (void)segmentAction:(id)sender{

      if([sender selectedSegmentIndex] == 0){
       //do something with segment 1
       NSLog(@"Segment 1 preesed");
      }else{
       //do something with segment 2
       NSLog(@"Segment 2 preesed");
      }
    }
Share:
20,000
Nithin
Author by

Nithin

Mobile application developer

Updated on June 04, 2022

Comments

  • Nithin
    Nithin almost 2 years

    Is it possible to add buttons to the navigation bar using the IPhone SDK?

    I already have 2 buttons in the navigation bar as leftBarButton and rightBarButton. I need 2 more buttons. How to implement that?

    Its not obligatory that i need them to be included in the navigation bar itself. But since the application contains only a table, i don't think it can be given elsewhere.

  • Nithin
    Nithin over 14 years
    by this method, is it possible to add more than 2 buttons to the navigation bar???
  • Benjamin Ortuzar
    Benjamin Ortuzar over 14 years
    You can add several segments in which you can perform actions as you need. Check the UICatalog code sample, and you will see it working in the iphone simulator with two options.