Question

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,

enter image description here

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.

No correct solution

OTHER TIPS

I guess you have to make the UISegmentedControll a UIBarButtonItem:

UPDATE: This Code makes the SegmentedControll centered:

- (void)viewDidLoad{
    self.title = nil;
    UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Personnal", @"Department", @"Company", nil]];
    [statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];

    NSMutableArray *buttonArray     = [[NSMutableArray alloc] init];
    UIBarButtonItem *flexibleSpace  = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [buttonArray addObject:flexibleSpace];
    [buttonArray addObject:[[UIBarButtonItem alloc] initWithCustomView:statFilter]];
    [buttonArray addObject:flexibleSpace];

    self.navigationItem.leftBarButtonItems = buttonArray;
    [buttonArray release];
}

I hope this helps you out!

Use this i hope this will help you

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView * navview = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 160,80)];
    self.navigationItem.leftBarButtonItem = nil;
    UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Personnal", @"Department", @"Company", nil]];
    [navview addSubview:statFilter];
    self.navigationItem.titleView = navview;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top