A simple and custom UISwitch made out of images.
- iOS 11.0+
- Xcode 9.0+
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods
CocoaPods 1.1.0+ is required to build ToggleSwitch 1.0+.
To integrate ToggleSwitch into your Xcode project using CocoaPods, specify it in your Podfile
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
pod 'ToggleSwitch', '~> 1.0'
Then, run the following command:
$ pod install
Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate ToggleSwitch into your Xcode project using Carthage, specify it in your Cartfile
:
github "ToggleSwitch/ToggleSwitch" ~> 1.0
To use ToggleSwitch as a Swift Package Manager package just add the following in your Package.swift file.
import PackageDescription
let package = Package(
name: "HelloToggleSwitch",
dependencies: [
.Package(url: "https://github.com/dimitris-c/ToggleSwitch.git", "1.0")
]
)
If you prefer not to use either of the aforementioned dependency managers, you can integrate ToggleSwitch into your project manually.
- Download the latest release from https://github.com/dimitris-c/ToggleSwitch/releases
- Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.
- In the tab bar at the top of that window, open the "General" panel.
- Click on the
+
button under the "Embedded Binaries" section. - Add the downloaded
ToggleSwitch.framework
. - And that's it!
Pretty standard integration as it's like adding a UISwitch.
let images = ToggleSwitchImages(baseOnImage: UIImage(named: "base_on"),
baseOffImage: UIImage(named: "base_off"),
thumbOnImage: UIImage(named: "thumb_on"),
thumbOffImage: UIImage(named: "thumb_off"))
// ToggleSwitch will use the baseOnImage to construct the size of the control
let onOffSwitch = ToggleSwitch(with: images)
onOffSwitch.frame.origin = CGPoint(x: 100, y: 100)
self.addSubview(onOffSwitch)
The control exposes two ways of retrieving when the value/state has changed.
onOffSwitch.stateChange = { isOn in
if isOn {
// do something
}
}
onOffSwitch.addTarget(self, action: #selector(toggleValueChanged), for: .valueChanged)
@objc func toggleValueChanged(control: ToggleSwitch) {
if onOffSwitch.isOn {
// do something
}
}
Similar to UISwitch, ToggleSwitch exposes isOn
and setOn(on:animated:)
method
onOffSwitch.isOn = true
onOffSwitch.setOn(on: false, animated: true)
ToggleSwitch is released under the MIT license. See LICENSE for details.