CocoaPods trunk is moving to be read-only. Read more on the blog, there are 18 months to go.

ToggleSwitch 1.2.1

ToggleSwitch 1.2.1

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Aug 2017
SwiftSwift Version 3.0.2
SPMSupports SPM

Maintained by Dimitris C..



  • By
  • Dimitris C.

BuddyBuild

A simple and custom UISwitch made out of images.

Requirements

  • iOS 9.0+
  • Xcode 8.0+

Installation

Swift Package Manager

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")
    ]
)

Manually

If you prefer not to use either of the aforementioned dependency managers, you can integrate ToggleSwitch into your project manually.

Embeded Binaries

  • 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!

Usage

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.

Using Block

onOffSwitch.stateChange = { isOn in 
    if isOn {
        // do something
    }
}

Using Target-Action

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)

License

ToggleSwitch is released under the MIT license. See LICENSE for details.