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 轉載請註明原做者及以上信息。