In this tutorial, I will show how to add a UISegmentedControl to the navigationBar of your UINavigationController.
First, we need to decide what is going to be in our UISegmentedControl. Let’s use 2 icons from Glyphish Free (http://www.glyphish.com/): 28-star.png and 29-heart.png.
In your Navigation Controller’s rootViewController, setup your UISegmentedControl with our icons:
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:[UIImage imageNamed:@"28-star.png"], [UIImage imageNamed:@"29-heart.png"], nil]];
Let’s set the initial selectedSegmentIndex to the star and change the segmentedControlStyle so that it matches nicely with the style of the navigationBar:
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
Now, let’s put our UISegmentedControl into the navigationBar as a navigationItem. We’ll set it as the rightBarButtonItem since the left is usually used to move backwards in the navigationController.
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
self.navigationItem.rightBarButtonItem = barButtonItem;
And there you have it! You should have a nice Segmented Control that’s part of your Navigation Bar, like so:
