如何在iOS中自定义UISegmentedControl?

时间:2011-08-17 08:09:39

标签: iphone ios ipad

我正在定制UISegmentedControl,但我遇到了问题。

如何在UISegmentedControl中应用背景图像?更改色调颜色不符合我的要求。

由于

2 个答案:

答案 0 :(得分:5)

////Segmented Controll
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]];
        }
    }           
}

答案 1 :(得分:4)