I'm trying to add a UISegmentedControl
programmatically as the titleView
in a UINavigationController
. But it did not show up. Upon further investigation, I found out that the titleView
property is ignored if the leftBarButtonItem
is not set to nil
according to the Apple docs. So I set it to nil
but still the segmented control does not show up!
Below is my code.
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = nil;
UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Personnal", @"Department", @"Company", nil]];
[statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
self.navigationItem.titleView = statFilter;
}
What should I do? What am I missing here?
Thanks.
EDIT :
Thanks for all the responses. However, none of them worked for me so I took a deeper look. It must be the way I have laid out my application. I probably should have mentioned that earlier. Apologies.
What I'm creating is a Storyboard app. The main controller is a UITabBarController
. When a certain tab is clicked, I want to show a UITableViewController
. Since you cannot display a Navigation bar in a UITableViewController
, I found out that I must add a UINavigationController
and implement a UITableViewController
inside it. This is how my app basically look like,
That Dashboard View Controller is connected to a class inheriting from UINavigationController
class. Maybe this is where the issue resides?
Because I just created a small test app, put a UIViewController
, embedded in a UINavigationController
and wrote the code I have posted in my original question above inside the UIViewController
's ViewDidLoad and the segmented control shows up just fine. Now I don't know how to do the same in my main app because there's no other view controllers or anything connected to the navigation controller.
Sorry if I made things even worse. Please comment if anything is unclear.