使用UIAppearance进行自定义后,SegmentControl的标题会失真

时间:2012-08-19 04:25:59

标签: objective-c ios ios5 custom-controls uisegmentedcontrol

自定义后,文本位置失真..

enter image description here

用于自定义的代码:

+ (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];
}

1 个答案:

答案 0 :(得分: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]];
    }
 }           
}