Google Currents-style UISegmentedControl

Introducing HMSegmentedControl, a drop-in replacement for UISegmentedControl mimicking the style of the segmented control used in Google Currents for iOS.

Grab the code from GitHub: https://github.com/HeshamMegid/HMSegmentedControl

Usage

The code on GitHub includes a demo project showing how to use the control.

To use in your own project, first import HMSegmentedControl.m and HMSegmentedControl.h into your project, then add QuartzCore to your linked libraries.

The code below will create a segmented control with the default looks:

1
2
3
4
5
HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"One", @"Two", @"Three"]];
[segmentedControl setFrame:CGRectMake(10, 10, 300, 60)];
[segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
[segmentedControl setTag:1];
[self.view addSubview:segmentedControl];

If you used this code in your project or it was helpful to you, I would love to hear from you!

  • Raul

    Hi! I use this control but when I try to change the selected segment by code, it doesn’t do it! How can I change the selected segment index by code? Thanks

    Sorry if there’s any mistake, I’m spanish ^^

    [segmentedControlTonos setSelectedSegmentIndex:6 animated:YES]; // Here’s the code that I used, but it doesn’t work :(

  • HeshamMegid

    Hi Raul,

    Yes, the code you’re using is right and it’s working fine. There’s probably something wrong somewhere else.

    Could you please share the code that you use to initialise the segmented control and add it to your view?

  • Raul

    segmentedControlTonos = [[HMSegmentedControl alloc] initWithSectionTitles:@[@”Bee”, @”Coming”, @”Ding Dong”, @”Do”, @”Harp Call”, @”Sol”, @”Wow”]];
    [segmentedControlTonos setFrame:CGRectMake(18, 320, 560, 60)];
    [segmentedControlTonos addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
    containerViewTonos = [[HMSegmentedControlContainerView alloc] initWithHMSegmentedControl:segmentedControlTonos];
    [self.view addSubview:containerViewTonos];

    [self segmentedControlChangedValue:segmentedControlTonos];

    But where I am trying to change the selected segment index is:

    if ([tono isEqualToString:@”Bee”]) {
    [segmentedControlTonos setSelectedSegmentIndex:0 animated:YES];
    }else if ([tono isEqualToString:@”Coming”]) {
    [segmentedControlTonos setSelectedSegmentIndex:1 animated:YES];
    }else if ([tono isEqualToString:@”Ding dong”]) {
    [segmentedControlTonos setSelectedSegmentIndex:2 animated:YES];
    }else if ([tono isEqualToString:@”Do”]) {
    [segmentedControlTonos setSelectedSegmentIndex:3 animated:YES];
    }else if ([tono isEqualToString:@”HarpCall”]) {
    [segmentedControlTonos setSelectedSegmentIndex:4 animated:YES];
    }else if ([tono isEqualToString:@”Sol”]) {
    [segmentedControlTonos setSelectedSegmentIndex:5 animated:YES];
    }else if ([tono isEqualToString:@”Wow”]) {
    [segmentedControlTonos setSelectedSegmentIndex:6 animated:YES];
    }else{
    [segmentedControlTonos setSelectedSegmentIndex:0 animated:YES];
    }

    Here. Here I get the value of a string that is saved on NSUserDefaults. If the string is equal to any of those texts (Ding dong, Coming, etc), the selected segment change when I load the view (viewWillAppear)

    Thanks for your reply :)

  • HeshamMegid

    Sorry for the late reply.

    Your code seems fine. Try setting a breakpoint and inspecting the value you get from the NSUserDefaults.

  • Raul

    Solved! The error was in this line on viewDidLoad: [self segmentedControlChangedValue:segmentedControlTonos];

    Thanks!! ^^