UISegmentedControl in the Navigation Bar with the Back button

Isuru picture Isuru · Mar 13, 2013 · Viewed 18.4k times · Source

I'm adding a UISegmentedControl to the Navigation bar programatically where the titleView should be. But as Apple docs have mentioned under titleView, This property is ignored if leftBarButtonItem is not nil.

But I want to have the back button as well. Like they have illustrated in their own images!

enter image description here

Below is the code I add the UISegmentedControl.

self.navigationItem.leftBarButtonItem = nil;
UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Filter_Personnal", @"Filter_Department", @"Filter_Company", nil];
[statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
self.navigationItem.titleView = statFilter;

Is there another way to add a UISegmentedControl along with the Back button as well?

Thank you.

Answer

arun.s picture arun.s · Mar 13, 2013

Try this

Remove this line --- > self.navigationItem.leftBarButtonItem = nil;

Add this instead

UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Filter_Personnal", @"Filter_Department", @"Filter_Company", nil]];
[statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
[statFilter sizeToFit];
self.navigationItem.titleView = statFilter;

Only change is I have added this line :

[statFilter sizeToFit];

Hope this Helps !!!