Giter Site home page Giter Site logo

kmcgill88 / mcpicker-ios Goto Github PK

View Code? Open in Web Editor NEW
214.0 9.0 42.0 139 KB

McPicker is a customizable, closure driven UIPickerView drop-in solution with animations that is rotation ready.

License: MIT License

Swift 97.53% Ruby 2.47%
ios uipickerview swift uitoolbar popover animation swift-framework pickerview iphone ipad

mcpicker-ios's Introduction

McPicker

Build Status Version License Platform

About

McPicker is a UIPickerView drop-in solution with animations that is rotation ready. The more string arrays you pass, the more picker components you'll get. You can set custom label or use the defaults. McPicker can be presented as a Popover on iPhone or iPad using showAsPopover, as an inputView using McTextField or use the default slide up and down style show.

showAsPopover can be used to display from a UIView or UIBarButtonItem. showAsPopover will always be presented as a Popover, even when used on an iPhone.

Usage

To run the example project, clone the repo, and run pod install from the Example directory first.

Short Syntax

  • Normal - (Slide up from bottom)
McPicker.show(data: [["Kevin", "Lauren", "Kibby", "Stella"]]) {  [weak self] (selections: [Int : String]) -> Void in
    if let name = selections[0] {
        self?.label.text = name
    }
}
  • As Popover
let data: [[String]] = [["Kevin", "Lauren", "Kibby", "Stella"]]
McPicker.showAsPopover(data: data, fromViewController: self, barButtonItem: sender) { [weak self] (selections: [Int : String]) -> Void in
    if let name = selections[0] {
        self?.label.text = name
    }
}
  • As an inputView via McTextField
@IBOutlet weak var mcTextField: McTextField!
override func viewDidLoad() {
    let data: [[String]] = [["Kevin", "Lauren", "Kibby", "Stella"]]
    let mcInputView = McPicker(data: data)
    mcInputView.backgroundColor = .gray
    mcInputView.backgroundColorAlpha = 0.25
    mcTextField.inputViewMcPicker = mcInputView

    mcTextField.doneHandler = { [weak mcTextField] (selections) in
        mcTextField?.text = selections[0]!
    }
    mcTextField.selectionChangedHandler = { [weak mcTextField] (selections, componentThatChanged) in
        mcTextField?.text = selections[componentThatChanged]!
    }
    mcTextField.cancelHandler = { [weak mcTextField] in
        mcTextField?.text = "Cancelled."
    }
    mcTextField.textFieldWillBeginEditingHandler = { [weak mcTextField] (selections) in
        if mcTextField?.text == "" {
            // Selections always default to the first value per component
            mcTextField?.text = selections[0]
        }
    }
}

Customization

let data: [[String]] = [
    ["Sir", "Mr", "Mrs", "Miss"],
    ["Kevin", "Lauren", "Kibby", "Stella"]
]
let mcPicker = McPicker(data: data)

let customLabel = UILabel()
customLabel.textAlignment = .center
customLabel.textColor = .white
customLabel.font = UIFont(name:"American Typewriter", size: 30)!
mcPicker.label = customLabel // Set your custom label

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
// Set custom toolbar items
mcPicker.setToolbarItems(items: [fixedSpace, cancelButton, flexibleSpace, fireButton, fixedSpace])

mcPicker.toolbarItemsFont = UIFont(name:"American Typewriter", size: 17)!
mcPicker.toolbarButtonsColor = .white
mcPicker.toolbarBarTintColor = .darkGray
mcPicker.backgroundColor = .gray
mcPicker.backgroundColorAlpha = 0.50
mcPicker.pickerBackgroundColor = .gray
mcPicker.pickerSelectRowsForComponents = [
    0: [3: true],
    1: [2: true] // [Component: [Row: isAnimated]
]

if let barButton = sender as? UIBarButtonItem {
    // Show as Popover
    //
    mcPicker.showAsPopover(fromViewController: self, barButtonItem: barButton) { [weak self] (selections: [Int : String]) -> Void in
        if let prefix = selections[0], let name = selections[1] {
            self?.label.text = "\(prefix) \(name)"
        }
    }
} else {
    // Show Normal
    //
    mcPicker.show(doneHandler: { [weak self] (selections: [Int : String]) -> Void in
        if let prefix = selections[0], let name = selections[1] {
            self?.label.text = "\(prefix) \(name)"
        }
    }, cancelHandler: {
        print("Canceled Styled Picker")
    }, selectionChangedHandler: { (selections: [Int:String], componentThatChanged: Int) -> Void  in
        let newSelection = selections[componentThatChanged] ?? "Failed to get new selection!"
        print("Component \(componentThatChanged) changed value to \(newSelection)")
    })
}
The selections

McPicker's doneHandler passes back selections: [Int : String] as an argument. This is as simple as [<Component Index>: <String of Selection>] from the data you've passed in.

Requirements

  • iOS 8+
  • Swift 5.2
  • Xcode 12

Note: Starting in 0.5.1 McPicker uses the Swift 4 Compiler. Ensure the correct compiler is set in your project.. If you'd like to use Swift 3 use version <=0.5.0.

Installation

McPicker is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "McPicker"

Xcode 12+ - Swift 5.2 Support

pod 'McPicker', '~> 3.0.0'

Swift 4.2 Support

For Swift 4.2 support, please use version 2.0.0.

pod 'McPicker', '~> 2.0.0'

Author

Kevin McGill, [email protected]

License

McPicker is available under the MIT license. See the LICENSE file for more info.

mcpicker-ios's People

Contributors

jadsalhani avatar kmcgill88 avatar longvt96 avatar lukemmtt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mcpicker-ios's Issues

Number of items per selection issue

Hi. I wanna do something like this:

let data: [[String]] = [
["Communities", "Resources"],
["All", "Popular", "Newest", "Nearest"]
]

But the app crashes. How can I achieve this?

Thanks!

PS. I don't want this ;)
let data: [[String]] = [
["Communities", "Resources","",""],
["All", "Popular", "Newest", "Nearest"]
]

ToolbarItems Font For Other States

Hi , setting ToolbarItems Fonts is currently just set for the .normal state of the buttons and when you click it or longPress (hold) on it the font is changed to default for a moment or for the time you are holding it

Use of unresolved identifier 'mcTextField'

I copied handler from example :
self.txt.doneHandler = { [weak mcTextField] (selections) in mcTextField?.text = selections[0]! }

but I got error Use of unresolved identifier 'mcTextField'

Missing selected index

Could be interesting to have the index of the selected item.

I've done it locally directly in the McPicker.swift file here:

    public func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        self.pickerSelection[component] = pickerData[component][row]
        self.pickerSelection[1] = String(row)
    }

I then get the result in the done Handler:

            mcPicker.show { (selections: [Int : String]) -> Void in
                self.mySelection = myArray[Int(selections[1]!)!]
            }

Support Carthage

Thank you for McPicker! McPicker looks to be exactly what I need, except that it doesn't support Carthage:

$ carthage update --platform ios 
*** Skipped building McPicker-iOS due to the error:
Dependency "McPicker-iOS" has no shared framework schemes for any of the platforms: iOS

I will experiment with adding support in my forked repo, and if successful, I will file a MR.

realodData() on picker how?

Hi, since this doesn't require your typical picker, how can I reload my data?

I want to populate my data from firebase, but I can't update my array since there's no way to reloadData on an array.

Thanks :)

Should present McPicker on UIWindow instead of topViewController

Presenting on the UIWindow will cause the 'dimmerView' not cover all VC's. Now if inside of a UINavigation controller then then top of the nav controller isn't dimmed. Doing this should correct it.

Can use a snippet like this.

private var appWindow:UIWindow {
        get {
            return UIApplication.shared.keyWindow!
        }
    }

Spacing between components

Is it possible to change the default spacing between different components (just like the spacing between buttons in the toolbar)?

Add Apps Using McPicker to README

I, of course, use McPicker in some apps of mine. I want to start an Apps using McPicker list in the README. If you are using McPicker in your app and would like to have your app added. Please send a PR with your app name and a link!

Is there a way to get the selected row or index, besides the text

I was trying to use this to choose from a list of [id:String], but since it only accepts [[String]], I created two lists one of [id] and one of [[String]], I want to know if I can get the selected row (index) in order to get the id from the other list, thanks.
Note: I have repeating strings so IndexOf will not help

Xcode 12 build errors

Since updating and attempting to compile with Xcode 12 beta I've getting build errors from McPicker for these two functions
public class func flexibleSpace() -> McPickerBarButtonItem
public class func fixedSpace(width: CGFloat) -> McPickerBarButtonItem

Is there any way using like 'didselect'??

#I have two related picker view
first picker : [ [Alphabet, Numbers] ]
second picker : [ [a,b,c, ...] or [1,2,3, ...] ]
if I select 'Alphabet' at first picker, second picker show [a,b,c, ...]
and if select 'Numbers', show [1,2,3, ...]

Add Travis-CI

Works fine now however to improve stability, insure quality and entice others to contribute I want to add CI to this project.

Custom ToolbarItems

Hi , please provide some method to change default ToolbarItems text (done,cancel) and setToolBarItems method that gets and array of buttons is not working properly in viewing them have you checked them ?
how to add two custom toolbarItems like "done" and "cancel" but with custom styles ?

How can I make my McPickerView Multiline in swift?

I have an McPickerView how can I make it multiline? I have some long text.

Here is my code :
```
let customLabel = UILabel()
customLabel.textAlignment = .center
customLabel.lineBreakMode = .byWordWrapping
customLabel.numberOfLines = 0
customLabel.sizeToFit()
mcPickerView.label = customLabel


**but nothing changed it still one single line.**

trouble with AppCenter

Hi,

I got this issue while trying to build my app on AppCenter

CompileSwift normal armv7 (in target 'McPicker' from project 'Pods')
cd /Users/runner/work/1/s/YourFut/Pods
/Applications/Xcode_11.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -emit-bc /Users/runner/work/1/s/YourFut/Pods/McPicker/McPicker/Classes/McPicker.swift /Users/runner/work/1/s/YourFut/Pods/McPicker/McPicker/Classes/McPickerBarButtonItem.swift /Users/runner/work/1/s/YourFut/Pods/McPicker/McPicker/Classes/McPickerPopoverViewController.swift /Users/runner/work/1/s/YourFut/Pods/McPicker/McPicker/Classes/McTextField.swift -emit-module-path /Users/runner/Library/Developer/Xcode/DerivedData/YourFut-bcgsjxzsmxluocadjionpbjzwutd/Build/Intermediates.noindex/ArchiveIntermediates/YourFut/IntermediateBuildFilesPath/Pods.build/Release-iphoneos/McPicker.build/Objects-normal/armv7/McPicker.swiftmodule -emit-module-doc-path /Users/runner/Library/Developer/Xcode/DerivedData/YourFut-bcgsjxzsmxluocadjionpbjzwutd/Build/Intermediates.noindex/ArchiveIntermediates/YourFut/IntermediateBuildFilesPath/Pods.build/Release-iphoneos/McPicker.build/Objects-normal/armv7/McPicker.swiftdoc -serialize-...
/Users/runner/work/1/s/YourFut/Pods/McPicker/McPicker/Classes/McPickerBarButtonItem.swift:61:32: error: method does not override any method from its superclass
public override class func flexibleSpace() -> Self {
~~~~~~~~ ^

xCode is fine but the issue happen with AppCenter only. :(

it's too slow...

Complete photo selection
It is too slow to return to the view I called.

I do not know if it's just me or the problem with the simulator.

toolbarItemsFont toolbarButtonsColor not working

Hi , mcPicker.toolbarItemsFont = UIFont(name: "W_koodak", size: 17)! this line is not working if its called before setting the toolbarItems and if its called after setting them its working fine and also its true for toolbarButtonsColor and maybe some more

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.