我从来没有在没有 Interface Builder 的情况下将分段控件添加到底部工具栏。我将相同的分段控件添加到导航控件工具栏而没有此问题。我究竟做错了什么?谢谢你。
//enable UIToolbar
self.navigationController.toolbarHidden = NO;
//UISegmentedControl
NSArray *segmentedControlItems = [[NSArray alloc] initWithObjects:@"Day One",@"Day Two",nil];
self.segmentedCon = [[UISegmentedControl alloc] initWithItems:segmentedControlItems];
[segmentedControlItems release];
[self.segmentedCon addTarget:self action:@selector(daySegConIndexChange) forControlEvents:UIControlEventValueChanged];
[self.segmentedCon setSelectedSegmentIndex:0];
[self.segmentedCon setSegmentedControlStyle:UISegmentedControlStyleBar];
[self.segmentedCon setWidth:85.0 forSegmentAtIndex:0];
[self.segmentedCon setWidth:85.0 forSegmentAtIndex:1];
[self.segmentedCon setFrame:[self.navigationController.toolbar bounds]];
//bar button items
UIBarButtonItem *flexibaleSpaceBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *segBarBtn = [[UIBarButtonItem alloc] initWithCustomView:self.segmentedCon];
//nsarray of tool bar items
NSArray *toolbarItems = [NSArray arrayWithObjects:flexibaleSpaceBarButton,segBarBtn,flexibaleSpaceBarButton,nil];
[self setToolbarItems:toolbarItems animated:YES];
[segBarBtn release];