How to customize the UISegmentedControl in iOS?

16,624

Solution 1

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

Solution 2

////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]];
        }
    }           
}
Share:
16,624
user403015
Author by

user403015

Updated on June 30, 2022

Comments

  • user403015
    user403015 almost 2 years

    I am customizing the UISegmentedControl, but I got a problem.

    How to apply background image in the UISegmentedControl ? Changing the tint color does not fulfill my requirements.

    Thanks

  • user403015
    user403015 over 12 years
    I just checked, but it seems quite complicated. Is there any easier solution ?
  • Maximilian Liesegang
    Maximilian Liesegang over 12 years
    If you are a Registered Apple Developer there are maybe some new methods for UISegmentedControl in iOS 5.