Question

I'm working on an app that's supposed to work on both iOS 6 and iOS 7, and have the same flat design for both.

I'm trying to customize my UISegmentedControl to have borders, corner radius and all, but I can't figure out how to do so. I've only mange to have a flat background so far.

Does anyone have any advice to have an iOS 6 UISegmentedControl look like an iOS 7 one ?

EDIT :

I would like to have

enter image description here

instead of

enter image description here

Was it helpful?

Solution

You can use below code:

 // To set colour of text
        NSDictionary *attributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor];
        [segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];
        NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor];
        [segmentedControl setTitleTextAttributes:highlightedAttributes forState:UIControlStateHighlighted];

        // Change color of selected segment

        segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;

        UIColor *newTintColor = [UIColor colorWithRed: 255/255.0 green:100/255.0 blue:100/255.0 alpha:1.0];
        segmentedControl.tintColor = newTintColor;

        UIColor *newSelectedTintColor = [UIColor clearColor];
        [[[segmentedControl subviews] objectAtIndex:0] setTintColor:newSelectedTintColor];

And for seting rounded corners you can use below code:

// Add rounded yellow corner to segmented controll view
[segmentedControl.layer setCornerRadius:4.0f];
[segmentedControl.layer setBorderColor:[UIColor colorWithRed:1.0 green:0.7 blue:0.14 alpha:1.0].CGColor];
[segmentedControl.layer setBorderWidth:1.5f];
[segmentedControl.layer setShadowColor:[UIColor blackColor].CGColor];
[segmentedControl.layer setShadowOpacity:0.8];
[segmentedControl.layer setShadowRadius:3.0];
[segmentedControl.layer setShadowOffset:CGSizeMake(2.0, 2.0)];

OTHER TIPS

You can take a look at an example here or take a custom control and change its images to fit your need.

Here's an example of how you can subclass UISegmentedControl:

@implementation CustomSegmentedControl
-(id)initWithItems:(NSArray *)items
{
    self = [super initWithItems:items];
    if (self) {

        [self setDividerImage:[UIImage imageNamed:@"segmented-control-divider-none-selected"]
          forLeftSegmentState:UIControlStateNormal
            rightSegmentState:UIControlStateNormal
                   barMetrics:UIBarMetricsDefault];
        [self setDividerImage:[UIImage imageNamed:@"segmented-control-divider-left-selected"]
          forLeftSegmentState:UIControlStateSelected
            rightSegmentState:UIControlStateNormal
                   barMetrics:UIBarMetricsDefault];
        [self setDividerImage:[UIImage imageNamed:@"segmented-control-divider-right-selected"]
          forLeftSegmentState:UIControlStateNormal
            rightSegmentState:UIControlStateSelected
                   barMetrics:UIBarMetricsDefault];

    // Set background images
    UIImage *normalBackgroundImage = [UIImage imageNamed:@"segmented-control-normal"];
    [self setBackgroundImage:normalBackgroundImage
                    forState:UIControlStateNormal
                  barMetrics:UIBarMetricsDefault];
    UIImage *selectedBackgroundImage = [UIImage imageNamed:@"segmented-control-selected"];
    [self setBackgroundImage:selectedBackgroundImage
                    forState:UIControlStateSelected
                  barMetrics:UIBarMetricsDefault];

    [self setBackgroundImage:selectedBackgroundImage
                    forState:UIControlStateHighlighted
                  barMetrics:UIBarMetricsDefault];

    double dividerImageWidth = [self dividerImageForLeftSegmentState:UIControlStateHighlighted rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault].size.width;
        [self setContentPositionAdjustment:UIOffsetMake(dividerImageWidth / 2, 0)
                            forSegmentType:UISegmentedControlSegmentLeft
                                barMetrics:UIBarMetricsDefault];
        [self setContentPositionAdjustment:UIOffsetMake(- dividerImageWidth / 2, 0)
                            forSegmentType:UISegmentedControlSegmentRight
                                barMetrics:UIBarMetricsDefault];
return self;
}

I've finally used this submodule. It does the work :

https://github.com/pepibumur/PPiFlatSegmentedControl

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top