已弃用,请使用 UIColorPickerViewController / ColorPicker 代替。
EFColorPicker 是一个轻量级的 Swift 颜色选择器,灵感来源于 MSColorPicker。
iOS 颜色选择器组件。它允许用户使用颜色组件选择颜色。主要特性
iPhone | iPad | |
---|---|---|
![]() |
![]() |
![]() |
要运行示例项目,请克隆仓库,并首先从 Example 目录运行 pod install
。
然后在 Xcode 中构建并运行 EFColorPicker.xcworkspace
,该演示展示了如何使用 EFColorPicker 并将其集成到您的项目中。
版本 | 需要 |
---|---|
<5.0 | Xcode 10.0+ Swift 4.2+ iOS 8.0+ |
5.x | Xcode 10.2+ Swift 5.0+ iOS 8.0+ |
EFColorPicker 可通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile
pod 'EFColorPicker'
您可以使用 Carthage 来安装 EFColorPicker
,方法是将其添加到您的 Cartfile
github "EFPrefix/EFColorPicker"
Swift Package Manager 是一个用于自动化 Swift 代码分发的工具,并已集成到 Swift 编译器中。
一旦您设置好 Swift 包,将 EFColorPicker 添加为依赖项就像将其添加到您的 Package.swift
的 dependencies
值一样容易。
dependencies: [
.package(url: "https://github.com/EFPrefix/EFColorPicker.git", .upToNextMinor(from: "5.2.2"))
]
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
colorSelectionController.setMode(mode: EFColorSelectionMode.all)
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 中使用 EFColorPicker
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
属性来控制颜色文本字段的可见性,例如
isColorTextFieldHidden: true | isColorTextFieldHidden: false | ||
---|---|---|---|
![]() |
![]() |
![]() |
![]() |
有关更多详细信息,请参阅演示。
EFColorSelectionViewControllerDelegate
,以便您可以感知颜色变化// 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)
}
![]() |
![]() |
![]() |
EyreFree, eyrefree@eyrefree.org
EFColorPicker 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。