Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A UISwitch-like segmented control for your iOS app.

branch: master

This branch is 0 commits ahead and 0 commits behind master

Fetching latest commit…

Cannot retrieve the latest commit at this time

README.textile

SVSegmentedControl

SVSegmentedControl is a customizable UIControl class that mimics UISegmentedControl but that looks like an UISwitch.

Installation

  • Drag the SVSegmentedControl/SVSegmentedControl folder into your project.
  • Add the QuartzCore framework to your project.

Usage

(see sample Xcode project in /Demo)

In its simplest form, this is how you create an SVSegmentedControl instance:

segmentedControl = [[SVSegmentedControl alloc] initWithSectionTitles:[NSArray arrayWithObjects:@"Section 1", @"Section 2", nil]];
[segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];

[self.view addSubview:segmentedControl];
[segmentedControl release];

You’re highly encouraged to position it using its center property:

segmentedControl.center = CGPointMake(160, 70);

Customization

SVSegmentedControl can be customized with the following properties:

@property (nonatomic, readwrite) NSUInteger selectedIndex; // default is 0
@property (nonatomic, retain) UIImage *backgroundImage; // default is nil
@property (nonatomic, retain) UIFont *font; // default is [UIFont boldSystemFontOfSize:15]
@property (nonatomic, retain) UIColor *textColor; // default is [UIColor grayColor];
@property (nonatomic, retain) UIColor *shadowColor;  // default is [UIColor blackColor]
@property (nonatomic, readwrite) CGSize shadowOffset;  // default is CGSizeMake(0, -1)
@property (nonatomic, readwrite) UIEdgeInsets titleEdgeInsets; // default is UIEdgeInsetsMake(0, 10, 0, 10)
@property (nonatomic, readwrite) CGFloat height; // default is 32.0
@property (nonatomic, readwrite) BOOL crossFadeLabelsOnDrag; // default is NO

Its thumb (SVSegmentedThumb) can be customized as well:

@property (nonatomic, retain) UIImage *backgroundImage; // default is nil
@property (nonatomic, retain) UIImage *highlightedBackgroundImage; // default is nil
@property (nonatomic, retain) UIColor *tintColor; // default is [UIColor grayColor]
@property (nonatomic, retain) UIColor *textColor; // default is [UIColor whiteColor]
@property (nonatomic, retain) UIColor *shadowColor; // default is [UIColor blackColor]
@property (nonatomic, readwrite) CGSize shadowOffset; // default is CGSizeMake(0, -1)

Note: to customize the thumb’s appearance, you’ll have to set the properties through SVSegmentedControl’s thumb property. For instance, setting the thumb’s tintColor is done with:

segmentedControl.thumb.tintColor = someColor;

Responding to value changes

SVSegmentedControl is now an UIControl subclass (thanks Oliver Letterer) and so you respond to value changes using the standard addTarget:action:forControlEvents: method:

[mySegmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];

Providing an action method ending with a semicolon, the sender object is therefore made accessible:

- (void)segmentedControlChangedValue:(SVSegmentedControl*)segmentedControl {
	NSLog(@"segmentedControl did select index %i", segmentedControl.selectedIndex);
}

If you’re a big fan of blocks, you can also respond to value changes using a block handler:

segmentedControl.selectedSegmentChangedHandler = ^(id sender) {
	SVSegmentedControl *segmentedControl = (SVSegmentedControl *)sender;
	NSLog(@"segmentedControl did select index %i", segmentedControl.selectedIndex);
};

Credits

SVSegmentedControl is brought to you by Sam Vermette (@samvermette) and contributors to the project. If you have feature suggestions or bug reports, feel free to help out by sending pull requests or by creating new issues. If you’re using SVSegmentedControl in your project, proper attribution would be nice.

Something went wrong with that request. Please try again.