Git Product home page Git Product logo

pvswitch's Introduction

PVSwitch

CI Status Version License Platform

Overview

PVSwitch is a Customizable Switch with the design inspired by Android's Switch.

This library has customizable properties that can be tweaked right from the Storyboard i.e. Inspectable Properties, making you play around with the Switch UI at compile time.

                 

Installation

  • CocoaPods
    PVSwitch is available on CocoaPods. You can use this library by adding the following command onto your Podfile:
pod 'PVSwitch'
  • Manual
    Just Drag+Drop the Source file into yor project. PVSwitch/Source/PVSwitch.swift

Usage


  • Storybord
    The simplest way to use PVSwitch is from the Storyboard. Just drag a view into the Storyboard and in the Identity Inspector (⌘ ⌥ 3) and set the Class field in Custom Class section to PVSwitch. The storyboard refreshes itself to bring up the inspectable properties to fiddle around with.


  • Code
    Adding PVSwitch through the code is as easy as using it from the Storyboard.
    Considering our ViewController has a Container View and PVSwitch has to be added as a subview.
class ViewController: UIViewController {
	let switchByCode = PVSwitch()
	@IBOutlet var containerView: UIView!

	override func viewDidLoad() {
		super.viewDidLoad()
		self.setupSwitch()
	}
}

Setting up the Switch

private func setupSwitch() {
	//Customize the Properties if the Switch is added by Code
	switchByCode.isOn = true

	switchByCode.isBounceEnabled = false

	switchByCode.thumbOnTintColor = .white
	switchByCode.trackOnTintColor = .green

	switchByCode.thumbOffTintColor = .darkGray
	switchByCode.trackOffTintColor = .lightGray

        //To handle the events
	switchByCode.addTarget(self, action: #selector(ViewController.codeSwitchAction(sender:)), for: .valueChanged)
	containerView.addSubview(switchByCode)
}

One last thing to do for this to work. In viewDidLayoutSubviews make sure to update the frames of the PVSwitch to be the container's bounds.

override func viewDidLayoutSubviews() {
	super.viewDidLayoutSubviews()

	//Make sure to set the frame of the switch if the Switch is added by Code
	switchByCode.frame = containerView.bounds
}

Change History

1.0.0 Initial Release

Requirements

iOS 11.0 or later

Author

Pulkit Vaid

License

PVSwitch is available under the MIT license.

Credits

This Switch is inspired by JTMaterialSwitch

pvswitch's People

Contributors

pulkit-vaid avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pvswitch's Issues

Change Switch value(isOn property) programatically

When trying to change isOn property programmatically, it changes Switch color successfully, but however it does not animate back the switch to its corresponding state position.

What I'm doing is:
1- Initialize a Switch with an OFF state.
2- Turn Switch ON with a gesture.
3- Then for some reason I need to programmatically turn Switch OFF again, so I do switch.isOn = false.

And I see what I detailed before. The switch isn't getting back to it's full OFF state position

unrecognized selector sent to instance

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PVSwitch.PVSwitch setThumbTintColor:]: unrecognized selector sent to instance 0x10678d730'

I try to add PVSwitch with pod

Then create programmatically and try to add navigation bar..
switchByCode = PVSwitch()
switchByCode!.addTarget(self, action: #selector(viewModeSwitchChanged(_:)), for: .valueChanged)
let switch_display = UIBarButtonItem(customView: switchByCode!)
navigationItem.rightBarButtonItem = switch_display
I got error.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.