Latest on twitter from msanchezmora 
  • loading...

Navigation Bar with a Segmented Button in the middle

Posted: April 15th, 2010 | Author: | Filed under: Objective-C | Tags: , , , | No Comments »

We can change the buttons on a navigation bar on the left and right sides, but how do we add a button in the middle of the Navigation Bar? The trick is to use the “titleView” property of navigationItem, and to assign it the customView of our just created segmented control. For more clarity look at the code below. Feel free to comment!

  1.  
  2. NSArray *itemArray = [NSArray arrayWithObjects: @"All", @"Some", nil];
  3. chooseAllORSome = [[UISegmentedControl alloc] initWithItems:itemArray];
  4. chooseAllORSome = CGRectMake(0,0,150, 32);
  5. chooseAllORSome.segmentedControlStyle = UISegmentedControlStyleBar;
  6. chooseAllORSome.selectedSegmentIndex = 0;
  7. [chooseAllORSome addTarget:self action:@selector(pickOne:)  forControlEvents:UIControlEventValueChanged];
  8. UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView: chooseAllORSome];
  9. self.navigationItem.titleView = segmentBarItem.customView;
  10. self.navigationItem.title = @"";
  11. [segmentBarItem release];
  12.  

Post to Twitter