EFColorPicker 是一个纯 Swift 的轻量级 iOS 颜色选择器,受 MSColorPicker 启发。git
English Introductiongithub
iOS 颜色选择器组件,它可以让用户选择自定义颜色,关键特性以下:bash
iPhone | iPad | |
---|---|---|
![]() |
![]() |
![]() |
git clone
命令下载本仓库;pod install
命令;EFColorPicker.xcworkspace
编译便可。或执行如下命令:ide
git clone git@github.com:EyreFree/EFColorPicker.git; cd EFColorPicker/Example; pod install; open EFColorPicker.xcworkspace
复制代码
EFColorPicker 能够经过 CocoaPods 进行获取。只须要在你的 Podfile 中添加以下代码就能实现引入:post
pod "EFColorPicker"
复制代码
import EFColorPicker
复制代码
let colorSelectionController = EFColorSelectionViewController()
let navCtrl = UINavigationController(rootViewController: colorSelectionController)
navCtrl.navigationBar.backgroundColor = UIColor.white
navCtrl.navigationBar.isTranslucent = false
navCtrl.modalPresentationStyle = UIModalPresentationStyle.popover
navCtrl.popoverPresentationController?.delegate = self
navCtrl.popoverPresentationController?.sourceView = sender
navCtrl.popoverPresentationController?.sourceRect = sender.bounds
navCtrl.preferredContentSize = colorSelectionController.view.systemLayoutSizeFitting(
UILayoutFittingCompressedSize
)
colorSelectionController.delegate = self
colorSelectionController.color = self.view.backgroundColor ?? UIColor.white
if UIUserInterfaceSizeClass.compact == self.traitCollection.horizontalSizeClass {
let doneBtn: UIBarButtonItem = UIBarButtonItem(
title: NSLocalizedString("Done", comment: ""),
style: UIBarButtonItemStyle.done,
target: self,
action: #selector(ef_dismissViewController(sender:))
)
colorSelectionController.navigationItem.rightBarButtonItem = doneBtn
}
self.present(navCtrl, animated: true, completion: nil)
复制代码
也能够经过 Storyboard 调用:ui
if "showPopover" == segue.identifier {
guard let destNav: UINavigationController = segue.destination as? UINavigationController else {
return
}
if let size = destNav.visibleViewController?.view.systemLayoutSizeFitting(UILayoutFittingCompressedSize) {
destNav.preferredContentSize = size
}
destNav.popoverPresentationController?.delegate = self
if let colorSelectionController = destNav.visibleViewController as? EFColorSelectionViewController {
colorSelectionController.delegate = self
colorSelectionController.color = self.view.backgroundColor ?? UIColor.white
if UIUserInterfaceSizeClass.compact == self.traitCollection.horizontalSizeClass {
let doneBtn: UIBarButtonItem = UIBarButtonItem(
title: NSLocalizedString("Done", comment: ""),
style: UIBarButtonItemStyle.done,
target: self,
action: #selector(ef_dismissViewController(sender:))
)
colorSelectionController.navigationItem.rightBarButtonItem = doneBtn
}
}
}
复制代码
你能够经过修改 EFColorSelectionViewController
的 isColorTextFieldHidden
属性来控制颜色编辑框的可见性,效果以下:spa
isColorTextFieldHidden: true | isColorTextFieldHidden: false | ||
---|---|---|---|
![]() |
![]() |
![]() |
![]() |
具体可参考示例程序。debug
// MARK:- EFColorSelectionViewControllerDelegate
func colorViewController(colorViewCntroller: EFColorSelectionViewController, didChangeColor color: UIColor) {
self.view.backgroundColor = color
// TODO: You can do something here when color changed.
print("New color: " + color.debugDescription)
}
复制代码
EFColorPicker 的第一个版本从 MSColorPicker 转换而来,在此对 MSColorPicker 的做者 sgl0v 表示感谢!3d
EyreFree, eyrefree@eyrefree.org
EFQRCode 基于 MIT 协议进行分发和使用,更多信息参见协议文件。
若有任何知识产权、版权问题或理论错误,还请指正。 https://juejin.im/post/5a32179ff265da431a43359d 转载请注明原做者及以上信息。