user594161
user594161

Reputation:

UISegmentedControl Within UIToolBar

I know how to add a UISegmentedControl to a UIToolBar from within IB, but I am trying to do the same programmatically, because I am using a custom subclass of UISegmentedControl with doesn't have an XIB.

This is the code for the UISegmentedControl:

SVSegmentedControl *navSC = [[SVSegmentedControl alloc] initWithSectionTitles:[NSArray arrayWithObjects:@"List", @"Calendar", nil]];
navSC.delegate = self;
[self.view addSubview:navSC];
[navSC release];
navSC.center = CGPointMake(160, 70);

I was thinking of doing something like [self.toolbar addSubview:navSC], but that didn't show anything.

Upvotes: 7

Views: 5850

Answers (1)

PengOne
PengOne

Reputation: 48398

You need to use the UIToolbar method – setItems:animated: (detailed in the documentation):

UIBarButtonItem *segItem = [[UIBarButtonItem alloc] initWithCustomView:navSC];
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
[toolBar setItems:[NSArray arrayWithObjects:spaceItem,segItem,spaceItem,nil] animated:YES];
[segItem release];
[spaceItem release];

Upvotes: 14

Related Questions