diff --git a/.swiftlint.yml b/.swiftlint.yml index 519b0ae..256e35a 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -1,2 +1,5 @@ line_length: 200 cyclomatic_complexity: 5 +file_length: + warning: 500 + error: 1200 diff --git a/Example/McPicker.xcodeproj/project.pbxproj b/Example/McPicker.xcodeproj/project.pbxproj index ec9b4cd..b16bad6 100644 --- a/Example/McPicker.xcodeproj/project.pbxproj +++ b/Example/McPicker.xcodeproj/project.pbxproj @@ -15,6 +15,7 @@ 607FACE01AFB9204008FA782 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 607FACDE1AFB9204008FA782 /* LaunchScreen.xib */; }; 607FACEC1AFB9204008FA782 /* McPickerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607FACEB1AFB9204008FA782 /* McPickerTests.swift */; }; D1E12A0ABE0D9540DA0C439E /* Pods_McPicker_Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1B6C05EDB200F71455915F2A /* Pods_McPicker_Example.framework */; }; + E7E851651F6B755400BC6A21 /* McPickerBarButtonItemTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E7E851631F6B736000BC6A21 /* McPickerBarButtonItemTests.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -45,6 +46,7 @@ 72A6ECE6754F0D53A01840EE /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = ""; }; ABDF864FC68406B6ADCF5DB4 /* Pods-McPicker_Tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-McPicker_Tests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-McPicker_Tests/Pods-McPicker_Tests.debug.xcconfig"; sourceTree = ""; }; D9855A22F5DA80A1D2B923E0 /* Pods_McPicker_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_McPicker_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + E7E851631F6B736000BC6A21 /* McPickerBarButtonItemTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = McPickerBarButtonItemTests.swift; sourceTree = ""; }; EC9DECD62B7A481490DA18FF /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = ""; }; ED86DD62AC5C0FB422C21828 /* Pods-McPicker_Example.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-McPicker_Example.debug.xcconfig"; path = "Pods/Target Support Files/Pods-McPicker_Example/Pods-McPicker_Example.debug.xcconfig"; sourceTree = ""; }; /* End PBXFileReference section */ @@ -127,6 +129,7 @@ isa = PBXGroup; children = ( 607FACEB1AFB9204008FA782 /* McPickerTests.swift */, + E7E851631F6B736000BC6A21 /* McPickerBarButtonItemTests.swift */, 607FACE91AFB9204008FA782 /* Supporting Files */, ); path = Tests; @@ -384,6 +387,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + E7E851651F6B755400BC6A21 /* McPickerBarButtonItemTests.swift in Sources */, 607FACEC1AFB9204008FA782 /* McPickerTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Example/McPicker.xcodeproj/xcshareddata/xcschemes/McPicker_Tests.xcscheme b/Example/McPicker.xcodeproj/xcshareddata/xcschemes/McPicker_Tests.xcscheme new file mode 100644 index 0000000..2e1b467 --- /dev/null +++ b/Example/McPicker.xcodeproj/xcshareddata/xcschemes/McPicker_Tests.xcscheme @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Example/McPicker/Base.lproj/Main.storyboard b/Example/McPicker/Base.lproj/Main.storyboard index 465e156..42d6dcf 100644 --- a/Example/McPicker/Base.lproj/Main.storyboard +++ b/Example/McPicker/Base.lproj/Main.storyboard @@ -1,5 +1,5 @@ - + diff --git a/Example/McPicker/ViewController.swift b/Example/McPicker/ViewController.swift index 2c285d6..1cda401 100644 --- a/Example/McPicker/ViewController.swift +++ b/Example/McPicker/ViewController.swift @@ -79,6 +79,13 @@ class ViewController: UIViewController { let mcPicker = McPicker(data: data) mcPicker.label = customLabel // Set your custom label mcPicker.toolbarItemsFont = UIFont(name:"American Typewriter", size: 17)! + + let fixedSpace = McPickerBarButtonItem.fixedSpace(width: 20.0) + let flexibleSpace = McPickerBarButtonItem.flexibleSpace() + let fireButton = McPickerBarButtonItem.done(mcPicker: mcPicker, title: "Fire!!!") // Set custom Text + let cancelButton = McPickerBarButtonItem.cancel(mcPicker: mcPicker, barButtonSystemItem: .cancel) // or system items + mcPicker.setToolbarItems(items: [fixedSpace, cancelButton, flexibleSpace, fireButton, fixedSpace]) + mcPicker.toolbarButtonsColor = .white mcPicker.toolbarBarTintColor = .darkGray mcPicker.pickerBackgroundColor = .gray diff --git a/Example/Tests/McPickerBarButtonItemTests.swift b/Example/Tests/McPickerBarButtonItemTests.swift new file mode 100644 index 0000000..00e0aa5 --- /dev/null +++ b/Example/Tests/McPickerBarButtonItemTests.swift @@ -0,0 +1,107 @@ +/* + Copyright (c) 2017 Kevin McGill + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + */ + +import UIKit +import XCTest +@testable import McPicker + +// swiftlint:disable nesting +// swiftlint:disable force_cast +class McPickerBarButtonItemTests: XCTestCase { + + let mcPicker = McPicker(data: [["Bob", "Bill"]]) + + func testInitDone_noTitleDefaultStyle() { + let item = McPickerBarButtonItem.done(mcPicker: mcPicker) + + XCTAssertNil(item.title) + XCTAssertTrue(item.style == .done) + XCTAssertTrue(item.target as! McPicker == mcPicker) + XCTAssertTrue(item.action == #selector(McPicker.done)) + } + + func testInitDone_noTitleCustomStyle() { + let item = McPickerBarButtonItem.done(mcPicker: mcPicker, barButtonSystemItem: UIBarButtonSystemItem.save) + + XCTAssertNil(item.title) + XCTAssertTrue(item.style == .plain) + XCTAssertTrue(item.target as! McPicker == mcPicker) + XCTAssertTrue(item.action == #selector(McPicker.done)) + } + + func testInitDone_withTitle() { + let title = "My Custom Title" + let item = McPickerBarButtonItem.done(mcPicker: mcPicker, title: title) + + XCTAssertTrue(item.title == title) + XCTAssertTrue(item.style == .plain) + XCTAssertTrue(item.target as! McPicker == mcPicker) + XCTAssertTrue(item.action == #selector(McPicker.done)) + } + + func testInitCancel_noTitle() { + let item = McPickerBarButtonItem.cancel(mcPicker: mcPicker) + + XCTAssertNil(item.title) + XCTAssertTrue(item.style == .plain) + XCTAssertTrue(item.target as! McPicker == mcPicker) + XCTAssertTrue(item.action == #selector(McPicker.cancel)) + } + + func testInitCancel_noTitleCustomStyle() { + let item = McPickerBarButtonItem.cancel(mcPicker: mcPicker, barButtonSystemItem: UIBarButtonSystemItem.save) + + XCTAssertNil(item.title) + XCTAssertTrue(item.style == .plain) + XCTAssertTrue(item.target as! McPicker == mcPicker) + XCTAssertTrue(item.action == #selector(McPicker.cancel)) + } + + func testInitCancel_withTitle() { + let title = "My Custom Title" + let item = McPickerBarButtonItem.cancel(mcPicker: mcPicker, title: title) + + XCTAssertTrue(item.title == title) + XCTAssertTrue(item.style == .plain) + XCTAssertTrue(item.target as! McPicker == mcPicker) + XCTAssertTrue(item.action == #selector(McPicker.cancel)) + } + + func testInitFlexibleSpace() { + let item = McPickerBarButtonItem.flexibleSpace() + + XCTAssertTrue(item.style == .plain) + XCTAssertNil(item.target) + XCTAssertNil(item.action) + } + + func testInitFixedSpace() { + let expectedWidth: CGFloat = 2.0 + let item = McPickerBarButtonItem.fixedSpace(width: expectedWidth) + + XCTAssertTrue(item.width == expectedWidth) + XCTAssertTrue(item.style == .plain) + XCTAssertNil(item.title) + XCTAssertNil(item.target) + XCTAssertNil(item.action) + } +} diff --git a/Example/Tests/McPickerTests.swift b/Example/Tests/McPickerTests.swift index b20a98c..7811168 100644 --- a/Example/Tests/McPickerTests.swift +++ b/Example/Tests/McPickerTests.swift @@ -54,11 +54,22 @@ class McPickerTests: XCTestCase { XCTAssertEqual(1, mcPicker.gestureRecognizers?.count) XCTAssertEqual(5, mcPicker.toolbar.items?.count) - XCTAssertEqual(mcPicker.fixedSpace.style, mcPicker.toolbar.items?[0].style) - XCTAssertEqual(mcPicker.cancelBarButton, mcPicker.toolbar.items?[1]) - XCTAssertEqual(mcPicker.flexibleSpace.style, mcPicker.toolbar.items?[2].style) - XCTAssertEqual(mcPicker.doneBarButton, mcPicker.toolbar.items?[3]) - XCTAssertEqual(mcPicker.fixedSpace.style, mcPicker.toolbar.items?[0].style) + + let fixedSpace = mcPicker.toolbar.items?[0] as! McPickerBarButtonItem + XCTAssertNil(fixedSpace.target) + XCTAssertNil(fixedSpace.action) + + let cancelBarButton = mcPicker.toolbar.items?[1] as! McPickerBarButtonItem + XCTAssertEqual(cancelBarButton.target as! TestMcPicker, mcPicker) + XCTAssertEqual(cancelBarButton.action!, #selector(TestMcPicker.cancel)) + + let flexibleSpace = mcPicker.toolbar.items?[2] as! McPickerBarButtonItem + XCTAssertNil(flexibleSpace.target) + XCTAssertNil(flexibleSpace.action) + + let doneBarButton = mcPicker.toolbar.items?[3] as! McPickerBarButtonItem + XCTAssertEqual(doneBarButton.target as! TestMcPicker, mcPicker) + XCTAssertEqual(doneBarButton.action!, #selector(TestMcPicker.done)) XCTAssertEqual(mcPicker.backgroundColor, UIColor.black.withAlphaComponent(0.75)) XCTAssertEqual(mcPicker.backgroundView.backgroundColor, UIColor.white) @@ -74,12 +85,15 @@ class McPickerTests: XCTestCase { XCTAssertEqual(data.first?.first, mcPicker.pickerSelection[0]) XCTAssertNil(mcPicker.mcPickerPopoverViewController) + XCTAssertNil(mcPicker.showsSelectionIndicator) } func testSetToolbarButtonsColorSetsCancelAndDone() { // Given // let mcPicker = McPicker(data: data) + let cancelBarButton = mcPicker.toolbar.items?[1] as! McPickerBarButtonItem + let doneBarButton = mcPicker.toolbar.items?[3] as! McPickerBarButtonItem // When // @@ -87,14 +101,16 @@ class McPickerTests: XCTestCase { // Then // - XCTAssertEqual(UIColor.purple, mcPicker.cancelBarButton.tintColor) - XCTAssertEqual(UIColor.purple, mcPicker.doneBarButton.tintColor) + XCTAssertEqual(UIColor.purple, cancelBarButton.tintColor) + XCTAssertEqual(UIColor.purple, doneBarButton.tintColor) } func testSetToolbarDoneButtonsColorSetsDone() { // Given // let mcPicker = McPicker(data: data) + let cancelBarButton = mcPicker.toolbar.items?[1] as! McPickerBarButtonItem + let doneBarButton = mcPicker.toolbar.items?[3] as! McPickerBarButtonItem // When // @@ -102,14 +118,16 @@ class McPickerTests: XCTestCase { // Then // - XCTAssertNil(mcPicker.cancelBarButton.tintColor) - XCTAssertEqual(UIColor.purple, mcPicker.doneBarButton.tintColor) + XCTAssertNil(cancelBarButton.tintColor) + XCTAssertEqual(UIColor.purple, doneBarButton.tintColor) } func testSetToolbarCancelButtonsColorSetsCancel() { // Given // let mcPicker = McPicker(data: data) + let cancelBarButton = mcPicker.toolbar.items?[1] as! McPickerBarButtonItem + let doneBarButton = mcPicker.toolbar.items?[3] as! McPickerBarButtonItem // When // @@ -117,8 +135,8 @@ class McPickerTests: XCTestCase { // Then // - XCTAssertEqual(UIColor.purple, mcPicker.cancelBarButton.tintColor) - XCTAssertNil(mcPicker.doneBarButton.tintColor) + XCTAssertEqual(UIColor.purple, cancelBarButton.tintColor) + XCTAssertNil(doneBarButton.tintColor) } func testSetToolbarCancelButtonColor() { @@ -140,6 +158,8 @@ class McPickerTests: XCTestCase { // Given // let mcPicker = McPicker(data: data) + let cancelBarButton = mcPicker.toolbar.items?[1] as! McPickerBarButtonItem + let doneBarButton = mcPicker.toolbar.items?[3] as! McPickerBarButtonItem let expectedFont = UIFont(name:"American Typewriter", size: 17)! // When @@ -148,8 +168,8 @@ class McPickerTests: XCTestCase { // Then // - XCTAssertEqual(expectedFont, mcPicker.cancelBarButton.titleTextAttributes(for: .normal)?[NSFontAttributeName] as! UIFont) - XCTAssertEqual(expectedFont, mcPicker.doneBarButton.titleTextAttributes(for: .normal)?[NSFontAttributeName] as! UIFont) + XCTAssertEqual(expectedFont, cancelBarButton.titleTextAttributes(for: .normal)?[NSFontAttributeName] as! UIFont) + XCTAssertEqual(expectedFont, doneBarButton.titleTextAttributes(for: .normal)?[NSFontAttributeName] as! UIFont) } func testSetPickerBackgroundColor() { @@ -251,4 +271,18 @@ class McPickerTests: XCTestCase { XCTAssertEqual("Miss", mcPicker.pickerSelection[0]!) XCTAssertEqual("Kibby", mcPicker.pickerSelection[1]!) } + + func testPickerCanSetShowsSelectionIndicator() { + // Given + // + let mcPicker = McPicker(data: data) + + // When + // + mcPicker.showsSelectionIndicator = true + + // Then + // + XCTAssertTrue(mcPicker.showsSelectionIndicator!) + } } diff --git a/McPicker.podspec b/McPicker.podspec index 6f0e779..518724d 100644 --- a/McPicker.podspec +++ b/McPicker.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'McPicker' - s.version = '0.4.0' + s.version = '0.5.0' s.summary = 'McPicker is a UIPickerView drop-in solution with animations that is rotation ready.' s.description = <<-DESC diff --git a/McPicker/Classes/McPicker.swift b/McPicker/Classes/McPicker.swift index 47fdb33..384747f 100644 --- a/McPicker/Classes/McPicker.swift +++ b/McPicker/Classes/McPicker.swift @@ -25,56 +25,74 @@ import UIKit open class McPicker: UIView { open var fontSize: CGFloat = 25.0 + + /** + The custom label to use with the picker. + + ``` + let customLabel = UILabel() + customLabel.textAlignment = .center + customLabel.textColor = .white + customLabel.font = UIFont(name:"American Typewriter", size: 30)! + + mcPicker.label = customLabel // Set your custom label + ``` + */ open var label: UILabel? public var toolbarButtonsColor: UIColor? { didSet { - cancelBarButton.tintColor = toolbarButtonsColor - doneBarButton.tintColor = toolbarButtonsColor + applyToolbarButtonItemsSettings { (barButtonItem) in + barButtonItem.tintColor = toolbarButtonsColor + } } } public var toolbarDoneButtonColor: UIColor? { didSet { - doneBarButton.tintColor = toolbarDoneButtonColor + applyToolbarButtonItemsSettings(withAction: #selector(McPicker.done)) { (barButtonItem) in + barButtonItem.tintColor = toolbarDoneButtonColor + } } } public var toolbarCancelButtonColor: UIColor? { didSet { - cancelBarButton.tintColor = toolbarCancelButtonColor + applyToolbarButtonItemsSettings(withAction: #selector(McPicker.cancel)) { (barButtonItem) in + barButtonItem.tintColor = toolbarCancelButtonColor + } } } public var toolbarBarTintColor: UIColor? { - didSet { - toolbar.barTintColor = toolbarBarTintColor - } + didSet { toolbar.barTintColor = toolbarBarTintColor } } public var toolbarItemsFont: UIFont? { didSet { - for item in toolbar.items ?? [] { - item.setTitleTextAttributes([NSFontAttributeName: toolbarItemsFont!], for: .normal) + applyToolbarButtonItemsSettings { (barButtonItem) in + barButtonItem.setTitleTextAttributes([NSFontAttributeName: toolbarItemsFont!], for: .normal) } } } public var pickerBackgroundColor: UIColor? { - didSet { - picker.backgroundColor = pickerBackgroundColor - } + didSet { picker.backgroundColor = pickerBackgroundColor } } /** - Sets the picker's components row position and picker selections to those String values. + Sets the picker's components row position and picker selections to those String values. - [Int:[Int:Bool]] equates to [Component: [Row: isAnimated] + [Int:[Int:Bool]] equates to [Component: [Row: isAnimated] */ public var pickerSelectRowsForComponents: [Int: [Int: Bool]]? { didSet { for component in pickerSelectRowsForComponents!.keys { - if let row = pickerSelectRowsForComponents![component]?.keys.first, let isAnimated = pickerSelectRowsForComponents![component]?.values.first { + if let row = pickerSelectRowsForComponents![component]?.keys.first, + let isAnimated = pickerSelectRowsForComponents![component]?.values.first { pickerSelection[component] = pickerData[component][row] picker.selectRow(row, inComponent: component, animated: isAnimated) } } } } + public var showsSelectionIndicator: Bool? { + didSet { picker.showsSelectionIndicator = showsSelectionIndicator ?? false } + } internal var popOverContentSize: CGSize { return CGSize(width: Constant.pickerHeight + Constant.toolBarHeight, height: Constant.pickerHeight + Constant.toolBarHeight) @@ -82,19 +100,11 @@ open class McPicker: UIView { internal var pickerSelection: [Int:String] = [:] internal var pickerData: [[String]] = [] internal var numberOfComponents: Int { - return pickerData.count + return pickerData.count } internal let picker: UIPickerView = UIPickerView() internal let backgroundView: UIView = UIView() internal let toolbar: UIToolbar = UIToolbar() - internal let doneBarButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(done)) - internal let cancelBarButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancel)) - internal let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) - internal var fixedSpace: UIBarButtonItem { - let fixedSpaceBarButtonItem = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil) - fixedSpaceBarButtonItem.width = appWindow.bounds.size.width * 0.02 - return fixedSpaceBarButtonItem - } internal var isPopoverMode = false internal var mcPickerPopoverViewController: McPickerPopoverViewController? internal enum AnimationDirection { @@ -117,6 +127,7 @@ open class McPicker: UIView { static let toolBarHeight: CGFloat = 44.0 static let backgroundAlpha: CGFloat = 0.75 static let animationSpeed: TimeInterval = 0.25 + static let barButtonFixedSpacePadding: CGFloat = 0.02 } convenience public init(data: [[String]]) { @@ -228,7 +239,7 @@ open class McPicker: UIView { fromViewController.present(mcPickerPopoverViewController!, animated: true) } - open func setToolbarItems(items: [UIBarButtonItem]) { + open func setToolbarItems(items: [McPickerBarButtonItem]) { toolbar.items = items } @@ -305,10 +316,22 @@ open class McPicker: UIView { } } + @objc internal func done() { + self.doneHandler(self.pickerSelection) + self.dismissViews() + } + + @objc internal func cancel() { + self.cancelHandler() + self.dismissViews() + } + private func setup() { self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(cancel))) - setToolbarItems(items: [fixedSpace, cancelBarButton, flexibleSpace, doneBarButton, fixedSpace]) + let fixedSpace = McPickerBarButtonItem.fixedSpace(width: appWindow.bounds.size.width * Constant.barButtonFixedSpacePadding) + setToolbarItems(items: [fixedSpace, McPickerBarButtonItem.cancel(mcPicker: self), + McPickerBarButtonItem.flexibleSpace(), McPickerBarButtonItem.done(mcPicker: self), fixedSpace]) self.backgroundColor = UIColor.black.withAlphaComponent(Constant.backgroundAlpha) backgroundView.backgroundColor = UIColor.white @@ -325,14 +348,16 @@ open class McPicker: UIView { } } - @objc private func done() { - self.doneHandler(self.pickerSelection) - self.dismissViews() - } + private func applyToolbarButtonItemsSettings(withAction: Selector? = nil, settings: (_ barButton: UIBarButtonItem) -> Void) { + for item in toolbar.items ?? [] { + if let action = withAction, action == item.action { + settings(item) + } - @objc private func cancel() { - self.cancelHandler() - self.dismissViews() + if withAction == nil { + settings(item) + } + } } } diff --git a/McPicker/Classes/McPickerBarButtonItem.swift b/McPicker/Classes/McPickerBarButtonItem.swift new file mode 100644 index 0000000..b643117 --- /dev/null +++ b/McPicker/Classes/McPickerBarButtonItem.swift @@ -0,0 +1,72 @@ +/* + Copyright (c) 2017 Kevin McGill + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + */ + +import UIKit + +open class McPickerBarButtonItem: UIBarButtonItem { + + /** + A bar button to close McPicker with selections. + + - parameter mcPicker: Target instance + - parameter title: Optionally set a custom title + - parameter barButtonSystemItem: Optionally set UIBarButtonSystemItem or omit for default: .done. NOTE: This option is ignored when title is non-nil. + + - returns: McPickerBarButtonItem + */ + public class func done(mcPicker: McPicker, title: String? = nil, barButtonSystemItem: UIBarButtonSystemItem = .done) -> McPickerBarButtonItem { + + if let buttonTitle = title { + return self.init(title: buttonTitle, style: .plain, target: mcPicker, action: #selector(McPicker.done)) + } + + return self.init(barButtonSystemItem: barButtonSystemItem, target: mcPicker, action: #selector(McPicker.done)) + } + + /** + A bar button to close McPicker with out selections. + + - parameter mcPicker: Target instance + - parameter title: Optionally set a custom title + - parameter barButtonSystemItem: Optionally set UIBarButtonSystemItem or omit for default: .done. NOTE: This option is ignored when title is non-nil. + + - returns: McPickerBarButtonItem + */ + public class func cancel(mcPicker: McPicker, title: String? = nil, barButtonSystemItem: UIBarButtonSystemItem = .cancel) -> McPickerBarButtonItem { + + if let buttonTitle = title { + return self.init(title: buttonTitle, style: .plain, target: mcPicker, action: #selector(McPicker.cancel)) + } + + return self.init(barButtonSystemItem: barButtonSystemItem, target: mcPicker, action: #selector(McPicker.cancel)) + } + + public class func flexibleSpace() -> McPickerBarButtonItem { + return self.init(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) + } + + public class func fixedSpace(width: CGFloat) -> McPickerBarButtonItem { + let fixedSpace = self.init(barButtonSystemItem: .fixedSpace, target: nil, action: nil) + fixedSpace.width = width + return fixedSpace + } +} diff --git a/README.md b/README.md index d90c5d8..b1a6e27 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,13 @@ customLabel.font = UIFont(name:"American Typewriter", size: 30)! let mcPicker = McPicker(data: data) mcPicker.label = customLabel // Set your custom label mcPicker.toolbarItemsFont = UIFont(name:"American Typewriter", size: 17)! + +let fixedSpace = McPickerBarButtonItem.fixedSpace(width: 20.0) +let flexibleSpace = McPickerBarButtonItem.flexibleSpace() +let fireButton = McPickerBarButtonItem.done(mcPicker: mcPicker, title: "Fire!!!") +let cancelButton = McPickerBarButtonItem.cancel(mcPicker: mcPicker, barButtonSystemItem: .cancel) +mcPicker.setToolbarItems(items: [fixedSpace, cancelButton, flexibleSpace, fireButton, fixedSpace]) + mcPicker.toolbarButtonsColor = .white mcPicker.toolbarBarTintColor = .darkGray mcPicker.pickerBackgroundColor = .gray