2013-02-13 80 views
0

我從來沒有添加一個分段控制到底部工具欄沒有界面生成器。我在導航控制工具欄中添加了相同的分段控件,但沒有此問題。我究竟做錯了什麼?謝謝。UIToolbar與異常脂肪UISegmentedControl

a screenshot of what im getting

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

回答

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]; 
+1

這工作。謝謝。我是否錯過了某些東西,或者只有一條線對我造成這種麻煩? '// this one'' '[self.segCon setFrame:[self.navigationController.toolbar bounds]];' 它是有道理的,它會將分段控件拉伸到工具欄的高度,即設置self.segCon導航到工具欄的邊界。每個索引的「setWidth:85.0」都塞進左邊。 – Airuop 2013-02-14 03:25:18

+1

你是對的!只是一條線.. – chuthan20 2013-02-14 04:57:56

0

嘗試設置分段控制的框架...

CGRect r = self.segmentedCon.frame; 

self.segmentedCon.frame = CGRectMake(r.origin.x, r.origin.y, r.size.width, 30); 
+0

相同的結果。 – Airuop 2013-02-13 04:54:21