2011-09-07 32 views
0

我UISegmentedControl工具欄上,當我添加自定義背景畫法工具欄分段控制移動近2px的下降,所以它不是垂直居中。下面是相應的圖像鏈接,請參閱screenshot和代碼的底部,我是如何加入分段控制程序工具欄:iPhone:UISegmentedController定製UIToolBar下移

segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentedItems]; 
[segmentedControl addTarget:self action:@selector(tabChanged:) forControlEvents:UIControlEventValueChanged]; 

UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
button1.style = UIBarButtonItemStylePlain; 
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl]; 
button2.width = kSegmentedControlFrame.size.width; 
button2.style = UIBarButtonItemStylePlain; 
UIBarButtonItem *button3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
button3.style = UIBarButtonItemStylePlain; 

CGFloat y = self.contentView != nil ? self.contentView.frame.origin.y + self.contentView.frame.size.height : kToolbarFrame.origin.y; 
UIToolbar *t = [[[UIToolbar alloc] initWithFrame:CGRectMake(kToolbarFrame.origin.x, y, 
                  kToolbarFrame.size.width, kToolbarFrame.size.height)] autorelease]; 
[t setItems:[NSArray arrayWithObjects:button1, button2, button1 , nil]]; 
t.clipsToBounds = NO; 
t.autoresizesSubviews = YES; 
t.clearsContextBeforeDrawing = NO; 
t.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth 
        | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin; 
t.barStyle = UIBarStyleDefault; 
t.tintColor = [UIColor clearColor]; 
t.contentMode = UIViewContentModeScaleToFill; 
t.backgroundColor = [UIColor clearColor]; 

segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; 
segmentedControl.tintColor = [ColorUtil colorWithRed:129 withGreen:167 withBlue:186]; 
segmentedControl.frame = kSegmentedControlFrame; //CGRectMake(kSegmentedControlFrameX, kSegmentedControlY, kSegmentedControlWidth, kSegmentedControlHeight); 
segmentedControl.selectedSegmentIndex = 0; 
segmentedControl.contentVerticalAlignment = UIControlContentVerticalAlignmentTop; 
segmentedControl.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; 
segmentedControl.clipsToBounds = NO; 
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin; 
segmentedControl.center = CGPointMake(160, 22); 
segmentedControl.momentary= NO; 

[button1 release]; 
[button2 release]; 
[button3 release]; 

return t; 

由於提前, 奈拉

回答

0

難道工具欄延伸到屏幕的底部,以便分段控制器只出現在靠近底部?在這裏沒有明確的防範,因爲內容視圖和工具欄大小一起高於可見屏幕區域。試着用一個origin.y值是沿

CGFloat y = self.view.bounds.size.height - kToolbarFrame.size.height; 
+0

沒有,工具欄不延長了屏幕的底部線的東西它定位。分段控制向下移動1px,其origin.y座標自動設置爲8,我不知道垂直居中如何。 –