Navigation Bar with a Segmented Button in the middle
Posted: April 15th, 2010 | Author: admin | Filed under: Objective-C | Tags: iPhone, Navigation Bar, Objective-C, Segmented Button | 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!
-
-
NSArray *itemArray = [NSArray arrayWithObjects: @"All", @"Some", nil];
-
chooseAllORSome = [[UISegmentedControl alloc] initWithItems:itemArray];
-
chooseAllORSome = CGRectMake(0,0,150, 32);
-
chooseAllORSome.segmentedControlStyle = UISegmentedControlStyleBar;
-
chooseAllORSome.selectedSegmentIndex = 0;
-
[chooseAllORSome addTarget:self action:@selector(pickOne:) forControlEvents:UIControlEventValueChanged];
-
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView: chooseAllORSome];
-
self.navigationItem.titleView = segmentBarItem.customView;
-
self.navigationItem.title = @"";
-
[segmentBarItem release];
-