0

我从来没有在没有 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];
4

2 回答 2

3

试试这个。。

//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];

//bar button items
UIBarButtonItem *flexibaleSpaceBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *segBarBtn = [[UIBarButtonItem alloc] initWithCustomView:self.segmentedCon];
于 2013-02-13T19:22:43.860 回答
0

尝试设置分段控件的框架...

CGRect r = self.segmentedCon.frame;

self.segmentedCon.frame = CGRectMake(r.origin.x, r.origin.y, r.size.width, 30);
于 2013-02-13T03:02:26.913 回答