• Stars
    star
    432
  • Rank 100,650 (Top 2 %)
  • Language
    Objective-C
  • License
    Other
  • Created almost 12 years ago
  • Updated about 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fully customizable switch for iOS using images

TTSwitch

One switch to rule them all.

TTSwitch is a UISwitch replacement built with images. You can now fully customize its appearance to whatever you want. It also adds block support when the switch value is changed.

The switch also supports UIAppearance. You can globally setup all the TTSwitch appeareance and then anytime you create an instance it will already be styled.

 

Getting Started

Git submodule

Add the TTSwitch as a submodule to your project or download the code from the master branch here.

Simply add the TTSwitch.h + TTSwitch.m files in the TTSwitch folder to your Xcode project. Then #include "TTSwitch.h" in your source files where you want to use the TTSwitch.

CocoaPods

Add TTSwitch to your Podfile and pod install.

pod 'TTSwitch', '~> 0.0.5'

Resources

To make it easy to create your own custom switch we have included a PSD of the switches you see in the app. You are free to use these or make your own. You can also send this to your designer so that they will know how to design and cut the switches. We have also included a diagram of the layers of the switch so you can see how it is layed out.

Example Usage

UIAppearance

Globally setup the appeareance of all the TTSwitchs in your app.

[[TTSwitch appearance] setTrackImage:[UIImage imageNamed:@"round-switch-track"]];
[[TTSwitch appearance] setOverlayImage:[UIImage imageNamed:@"round-switch-overlay"]];
[[TTSwitch appearance] setTrackMaskImage:[UIImage imageNamed:@"round-switch-mask"]];
[[TTSwitch appearance] setThumbImage:[UIImage imageNamed:@"round-switch-thumb"]];
[[TTSwitch appearance] setThumbHighlightImage:[UIImage imageNamed:@"round-switch-thumb-highlight"]];
[[TTSwitch appearance] setThumbMaskImage:[UIImage imageNamed:@"round-switch-mask"]];
[[TTSwitch appearance] setThumbInsetX:-3.0f];
[[TTSwitch appearance] setThumbOffsetY:-3.0f];

Default switch

TTSwitch *switch = [[TTSwitch alloc] initWithFrame:(CGRect){ CGPointZero, { 76.0f, 27.0f } }];
switch.trackImage = [UIImage imageNamed:@"square-switch-track"];
switch.overlayImage = [UIImage imageNamed:@"square-switch-overlay"];
switch.thumbImage = [UIImage imageNamed:@"square-switch-thumb"];
switch.thumbHighlightImage = [UIImage imageNamed:@"square-switch-thumb-highlight"];

Switch with thumb bigger than track

TTSwitch *switch = [[TTSwitch alloc] initWithFrame:(CGRect){ CGPointZero, { 76.0f, 27.0f } }];
switch.trackImage = [UIImage imageNamed:@"square-switch-track"];
switch.overlayImage = [UIImage imageNamed:@"square-switch-overlay"];
switch.thumbImage = [UIImage imageNamed:@"square-switch-thumb"];
switch.thumbHighlightImage = [UIImage imageNamed:@"square-switch-thumb-highlight"];
switch.trackMaskImage = [UIImage imageNamed:@"square-switch-mask"];
switch.thumbOffsetY = -3.0f; // Set this to -3 to compensate for shadow 

Switch with shadow on left and right of thumb that doesn't go outside of frame

TTSwitch *switch = [[TTSwitch alloc] initWithFrame:(CGRect){ CGPointZero, { 76.0f, 27.0f } }];
switch.trackImage = [UIImage imageNamed:@"round-switch-track"];
switch.overlayImage = [UIImage imageNamed:@"round-switch-overlay"];
switch.thumbImage = [UIImage imageNamed:@"round-switch-thumb"];
switch.thumbHighlightImage = [UIImage imageNamed:@"round-switch-thumb-highlight"];
switch.trackMaskImage = [UIImage imageNamed:@"round-switch-mask"];
switch.thumbMaskImage = [UIImage imageNamed:@"round-switch-mask"];
switch.thumbOffsetY = -3.0f; // Set this to -3 to compensate for shadow 

Switch with labels for on/off text

TTSwitch *roundLabelSwitch = [[TTSwitch alloc] initWithFrame:(CGRect){ CGPointZero, { 76.0f, 28.0f } }];
// use normal setup and add
roundLabelSwitch.trackImage = [UIImage imageNamed:@"round-switch-track-no-text"];
roundLabelSwitch.labelsEdgeInsets = (UIEdgeInsets){ 3.0f, 10.0f, 3.0f, 10.0f };
roundLabelSwitch.onString = NSLocalizedString(@"ON", nil);
roundLabelSwitch.offString = NSLocalizedString(@"OFF", nil);
roundLabelSwitch.onLabel.textColor = [UIColor greenColor];
roundLabelSwitch.offLabel.textColor = [UIColor redColor];

Credits

TTSwitch was created by Scott Penrose(@scottpenrose) and Two Toasters(@twotoasters) in the development of Go Try It On.

License

TTSwitch is available under the WTFPL. See the LICENSE file for more info.

More Repositories

1

JazzyListView

Java
935
star
2

TWTSideMenuViewController

Side Menus for iOS 7
Objective-C
851
star
3

clusterkraf

A clustering library for the Google Maps Android API v2
Java
255
star
4

SectionCursorAdapter

Java
110
star
5

multi-column-list-adapter

Java
100
star
6

TTAlertView

Objective-C
82
star
7

watchface-template

Java
79
star
8

Toast

Tools and Utilities for Cocoa Development
Objective-C
67
star
9

RecyclerViewLib

An example implementation of Android's new RecyclerView.
Java
63
star
10

HorizontalImageScroller-Android

A horizontal image scroller widget for Android
Java
50
star
11

MaterialRangeSlider

Material inspired range slider
Java
42
star
12

Wear-MessageApiDemo

Java
36
star
13

zbar

git clone of zbar sdk
Objective-C
35
star
14

AnimatedPath

Objective-C
33
star
15

AndroidStyleGuidelines

31
star
16

AndrOAuth

Java
29
star
17

hoot

A powerful, flexible, lightweight Android library for dealing with RESTful endpoints.
Java
26
star
18

watchface-gears

Java
20
star
19

UISpec

Git SVN Clone of UISpec
Objective-C
19
star
20

UISpecRunner

Flexible CLI test runner for UISpec
Ruby
16
star
21

FramerateDemo

Objective-C
11
star
22

toastdroid

Java
9
star
23

HelloAndroidStudio

Java
8
star
24

objective_stack

A Rails template used by the fine folks at Two Toasters
Ruby
7
star
25

TWTCommon

Misc. downright useful classes & additions for iphone development (with three 20)
Objective-C
7
star
26

project-example

Example repository demonstrating the twotoasters development process
6
star
27

Android-Master-Class-ListenerAwareAsyncTask

Java
6
star
28

fbconnect-iphone

Facebook Connect for iPhone
Objective-C
5
star
29

AnalyticsTest

Java
5
star
30

chron

Java
2
star
31

resource_controller_extensions

Provides reasonable default handlers for XML, JSON, and FBML to ResourceController
Ruby
2
star
32

iPhone-Build-Scripts

client build and release build scripts for iphone projects
Ruby
1
star
33

facebook-ios-sdk-framework

*Unofficial* build of the Facebook iOS SDK Framework
Objective-C
1
star
34

RKCatalog-Server

Simple Sinatra backend for the RestKit RKCatalog Sample app
Ruby
1
star
35

brominet

Objective-C
1
star
36

Xcode-Templates

Objective-C
1
star
37

Volley

Java
1
star