1

自定义后,文字位置歪了。。

在此处输入图像描述

用于自定义的代码:

+ (void)customiseSegmentControl {
UIImage *selectedImage = [[UIImage imageNamed:@"SegmentSelected.png"]
                    stretchableImageWithLeftCapWidth:5 topCapHeight:1];
UIImage *unselectedImage = [[UIImage imageNamed:@"SegmentUnselected.png"]
                    stretchableImageWithLeftCapWidth:5 topCapHeight:1];

[[UISegmentedControl appearance] setBackgroundImage:selectedImage forState:(UIControlStateSelected|UIControlStateHighlighted) barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:unselectedImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}
4

1 回答 1

0

http://idevrecipes.com/2010/12/11/custom-segmented-controls/

检查此链接..和代码

NSArray *segmentTextContent = [NSArray arrayWithObjects:@"First",@"Second",@"Third",@"Forth", nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
segmentedControl.frame = CGRectMake(2, 5, 316, 35);

[segmentedControl addTarget:self action:@selector(segmentAction)forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.enabled = true;
segmentedControl.selectedSegmentIndex = 0;

// cutomize the font size inside segmentedControl
for (id segment in [segmentedControl subviews]) 
{
 for (id label in [segment subviews]) 
 {
    if ([label isKindOfClass:[UILabel class]])
    {
        [label setTextAlignment:UITextAlignmentCenter];
        [label setFont:[UIFont boldSystemFontOfSize:11]];
        //[label setTextColor:[UIColor greenColor]];
    }
 }           
}
于 2012-08-19T05:15:35.090 回答