A color picking controller built in Swift 4
Minimum OS Target -> iOS 9.0
I couldn't find any color picker made for swift which fulfilled my requirement of being able to pick a color visually as well as using the hex code, so I made one myself.
Carthage -
Add this to your cartfile.
github "rishabhkohli/SwiftColorPicker"
- Initialize, set delegate, set presentation style and present. ColorPickerController takes two arguments - id and currentColor. If you don't provide any arguments, the default arguments of id=0 and currentColor=UIColor.white will be taken.
let colorPickerController = ColorPickerController(id: 0, currentColor: UIColor.blue)
colorPickerController.delegate = self
colorPickerController.modalPresentationStyle = .formSheet
present(colorPickerController, animated: true, completion: nil)
- To get callback, adopt the delegate ColorPickerViewControllerDelegate in your class and add this function. It returns the same id that you initialized the ColorPickerController with. This way you can have multiple color pickers in the same class and get seperate callbacks from each of them.
func colorSelected(id: Int, color: UIColor) {
switch id {
case 0:
print(color.toHexString())
break
default:
break
}
}
Credits -
Color Map - https://stackoverflow.com/a/34142316/6886672
UIColor Extension - https://github.com/yeahdongcn/UIColor-Hex-Swift