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.开发者_StackOverflowcenter = CGPointMake(160, 70);
I was thinking of doing something like [self.toolbar addSubview:navSC]
, but that didn't show anything.
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];
精彩评论