iPhone:自定义UIToolBar上的UISegmentedController向下移动

时间:2011-09-07 14:59:58

标签: iphone ios

我在工具栏上有UISegmentedControl,当我为工具栏分段控件添加自定义背景绘制方法时,向下移动了近2px,因此它不是垂直居中的。这是相应的图片链接,请参阅screenshot底部的工具栏,并编码我如何将分段控件添加到工具栏:

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;

提前致谢, 奈拉

1 个答案:

答案 0 :(得分:0)

工具栏是否可能从屏幕底部延伸出来,以便分段控制器只显示在底部附近?这里没有明确的防范,因为内容视图和工具栏大小一起比可见屏幕区域高。尝试使用origin.y值来定位它,该值类似于

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