Coder Social home page Coder Social logo

pickerview's Introduction

PickerView

CocoaPods CocoaPods Version License Platform

PickerView is an easy to use and customize alternative to UIPickerView written in Swift. It was developed to provide a highly customizable experience, so you can implement your custom designed UIPickerView.

Requirements

It requires Xcode 8.0+ and Swift 3.0.

NOTE: When PickerView was first built it wasn't thought to support Objective-C projects, but the community demanded it and recently we've released the version 0.2.0 which supports Objective-C projects. After some tests we noticed some bugs when using from Objective-C, so we've this issue open and we need some help to fix that, so if you are making some adjustments to run in your Objective-C project, please, contribute with us to fix these problems. Thanks.

Your project deployment target must be iOS 8.0+

Installation

CocoaPods

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

pod "PickerView"

Manually

You can also install it manually just draging PickerView file to your project.

Usage

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

PickerView is very simple to use, you can use it with Interface Builder or through code, it works similar as UIPickerView you need to implement PickerViewDataSource and PickerViewDelegate protocols to provide the basic information and make it works.

You can customize the text appearance, the scrolling style (default or infinite scrolling) and the selection style (selection indicators and overlays with custom UIView's or even an UIImage to highlight the selected row).

Interface Builder

Drag a UIView to your view controller (and put the constraints if you want).

Putting PickerView on Interface Builder

Change the class to PickerView on the Identity Inspector and make the @IBOutlet connection.

Change the custom class.

From Code

Create a new PickerView.

let examplePicker = PickerView()
examplePicker.translatesAutoresizingMaskIntoConstraints = false

Add to your view and put your custom constraints.

view.addSubview(examplePicker)
view.addConstraints([yourCustomConstraints])

Set the PickerViewDataSource and PickerViewDelegate

Don't forget to set the datasource and delegate of your PickerView:

// ...
// The `examplePicker` below is the same that you created through code or connected via @IBOutlet
examplePicker.dataSource = self
examplePicker.delegate = self
// ...

Implement PickerViewDataSource

The PickerViewDataSource protocol consists in two required methods:

@objc public protocol PickerViewDataSource: class {
    func pickerViewNumberOfRows(_ pickerView: PickerView) -> Int
    func pickerView(_ pickerView: PickerView, titleForRow row: Int) -> String
}

You need to return the pickerViewNumberOfRows as we see below:

func pickerViewNumberOfRows(_ pickerView: PickerView) -> Int {
    return itemsThatYouWantToPresent.count 
}

And the title for each row:

func pickerView(_ pickerView: PickerView, titleForRow row: Int) -> String {
    let item = itemsThatYouWantToPresent[row]
    return item.text
}

Done. Now you'll need to implement one more protocol and then we are all set.

Implement PickerViewDelegate

The PickerViewDelegate consists in five methods:

@objc public protocol PickerViewDelegate: class {
    func pickerViewHeightForRows(_ pickerView: PickerView) -> CGFloat
    optional func pickerView(_ pickerView: PickerView, didSelectRow row: Int)
    optional func pickerView(_ pickerView: PickerView, didTapRow row: Int)
    optional func pickerView(_ pickerView: PickerView, styleForLabel label: UILabel, highlighted: Bool)
    optional func pickerView(_ pickerView: PickerView, viewForRow row: Int, highlighted: Bool, reusingView view: UIView?) -> UIView?
}

Firstly you must provide the pickerViewHeightForRows(_:):

func pickerViewHeightForRows(_ pickerView: PickerView) -> CGFloat {
    return 50.0 // In this example I'm returning arbitrary 50.0pt but you should return the row height you want.
}

Then is the method where you can do something with the row selected in PickerView:

func pickerView(_ pickerView: PickerView, didSelectRow row: Int) {
    let selectedItem = itemsThatYouWantToPresent[row] 
    print("The selected item is \(selectedItem.name)") 
}

PickerView enable the user to tap a visible row to select it. We've a delegate method to track this tap behavior, so pickerView(_: PickerView, didTapRow row: Int) is called only when the user selects a row by tapping it:

func pickerView(_ pickerView: PickerView, didTapRow row: Int) {
    print("The row \(row) was tapped by the user") 
}

The following method allows you to customize the label that will present your items in PickerView. Use the flag `highlighted' to provide a differrent style when the item is selected:

func pickerView(_ pickerView: PickerView, styleForLabel label: UILabel, highlighted: Bool) {
    label.textAlignment = .Center
    
    if highlighted { 
        label.font = UIFont.systemFontOfSize(25.0)
        label.textColor = view.tintColor
    } else {
        label.font = UIFont.systemFontOfSize(15.0)
        label.textColor = .lightGrayColor()
    }
}

If you want to provide a totally customized view instead of presenting just a row with a text label inside

func pickerView(_ pickerView: PickerView, viewForRow row: Int, highlighted: Bool, reusingView view: UIView?) -> UIView? {
    var customView = view
    
    // Verify if there is a view to reuse, if not, init your view. 
    if customView == nil {
        // Init your view
        customView = MyCustomView()
    }
    
    // **IMPORTANT**: As you are providing a totally custom view, PickerView doesn't know where to bind the data provided on PickerViewDataSource, so you will need to bind the data in this method. 
    customView.yourCustomTextLabel.text = itemsThatYouWantToPresent[row].text
    
    // Don't forget to make your style customizations for highlighted state 
    let alphaBasedOnHighlighted: CGFloat = highlighted ? 1.0 : 0.5
    customView.yourCustomTextLabel.alpha = alphaBasedOnHighlighted
    
    return customView
}

Label or Custom View?

Even if func pickerView(_ pickerView: PickerView, styleForLabel label: UILabel, highlighted: Bool) and func pickerView(_ pickerView: PickerView, viewForRow row: Int, highlited: Bool, reusingView view: UIView?) are optional methods in PickerViewDelegate you must implement at least one of them. If you want to present your data using only a label (which you can customize too), implement the first one, but if you want to present your data in a totally customized view, you should implement the second one.

NOTE: If you implement the two methods mentioned above, PickerView will always present your data using the custom view you provided.

Cool! You are ready to build and run your application with PickerView, it ill works with the default configurations and the text appearance you provided in PickerViewDelegate on the next section we will cover the scrolling and selection style customizations.

Custom Appearance

As you can customize the text appearance through PickerViewDelegate, you have two more options to customize your PickerView: scrolling style and selection style.

Scrolling Style

Is defined by the scrollingStyle property of PickerView that is of type ScrollingStyle a nested enum that has two member values: .Default and .Infinite. It is explicit and you might already know what it does, but let me explain better:

.Default: The default scrolling experience, the user can scroll through your PickerView item's but he will reach the bottom or the top of your picker when it doesn't have more items to show.

.Infinite: With this scrolling style the user will loop through the items of your PickerView and never reach the end, it provides an infinite scrolling experience.

Selection Style

Is defined by the selectionStyle property of PickerView that is of type SelectionStyle a nested enum that has four member values: .None, .DefaultIndicator, .Overlay and .Image. For your better understanding, follow the explanation below:

.None: Don't uses any aditional view to highlight the selection, only the highlighted label style customization provided by delegate.

.DefaultIndicator: Provides a simple selection indicator on the bottom of the highlighted row with full width and 2pt of height. The default color is its superview tintColor but you have free access to customize the DefaultIndicator through the defaultSelectionIndicator property.

.Overlay: Provide a full width and height (the height you provided on delegate) view that overlay the highlighted row. The default color is its superview tintColor and the alpha is set to 0.25, but you have free access to customize it through the selectionOverlay property.

Tip: You can set the alpha to 1.0 and background color to .clearColor() and add your custom selection view as a selectionOverlay subview to make it looks as you want (don't forget to properly add the constraints related to selectionOverlay to keep your experience with any screen size).

.Image: Provide a full width and height image view selection indicator (the height you provided on delegate) without any image. You must have a selection indicator as an image and set it to the image view through the selectionImageView property.

Who's Using It?

See what projects are using PickerView on our wiki page.

Contribute

Feel free to submit your pull request, suggest any update, report a bug or create a feature request.

Before you start contributing, please take a look on our contributing guide.

Just want to say hello? -> ofilipealvarenga at gmail.com

Contributors

Author: @filipealva

See the people who helps to improve this project: Contributors

Special thanks to: @bguidolim and @jairobjunior.

Starring is caring 🌟

If you liked the project and want it to continue, please star it. It is a way for me to know the impact of the project. Thank you <3

License

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

Donate

pickerview's People

Contributors

bguidolim avatar felipowsky avatar filipealva avatar jsinge97 avatar lfarah avatar monikasarota avatar n13 avatar pettymn avatar qiuzhifei 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pickerview's Issues

Need to scroll pickerView to see updated values.

Whenever I update the list of values I want in the PickerView I have to scroll the current value out of view before they update.

I'm changing the PresentationType to updater the list, is this correct or am I missing something?

Build failed: "No such module 'PickerView' in ExamplePickerViewController.swift

Hi Filipe, I want to use your custom picker view on an iOS app I designed. I tried to build your app (Swift 3.2, Xcode 9.1) and there was a Swift Compiler Error saying No Such Module 'PickerView' in ExamplePickerViewController.swift

screen shot 2017-12-21 at 2 54 01 pm

Sorry, I'm a bit new to iOS development and I feel like I'm not doing something right. Do you know what the problem is?

Empty, selectable entry is shown on iOS 10

Pod used: - PickerView (0.3.4)

We've integrated the example project and tested it on iOS 10 and iOS12. On iOS 10 an empty entry is shown before the first entry and it's selectable. I suspect this in an layout issue. Here's a screencast of the issue in action:

kapture 2018-10-11 at 11 33 07

`reloadPickerView()` Acting Weird

I'm using with iOS 11 and Xcode 9 and when reloadPickerView() seems to be called my pickerView starts to act strange. The selection is off center from the overlay I have on with the standard style and my conditions for highlighted aren't met. Everything seems to get shifted up in position also.

[IMPROVEMENT] Some properties are not working when using from Objective-C project.

Some people reach me out reporting about some properties which aren't working when PickerView is implemented in an Objective-C project.

The known properties with issues:

  • scrollingStyle: When its set to .Infinite, or rawValue 1, it causes a crash.
  • selectionStyle: Not working.
  • currentSelectedRow: Usually it sets a initial selected value to PickerView but its not appearing as a public property when exposed to Objective-C (even if it is declared as public in PickerView).

Unfortunately I can't fix this right now, so I'm opening this issue as a [help-wanted].

If you can help, please interact here and/or submit a pull request.

Thanks.

Crash Sometimes

Hi !
I meet a strange problem that "sometimes" it will crash due to beyond the data array.

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'

and the " bounds[0..1] " will be different every time, sometimes it's [0..0], sometimes it's [0..2] and [0..3],

hope you can help me !

Programmatically Select Row

Is there a way to programmatically scroll to a row or index, after the picker has loaded?
I need to scroll to specific items, after the view has loaded, based on what the user selects in that View Controller

I've tried objectPicker.currentSelectedRow = 0

Any help is appreciated.

Objective.c version

This project is amazing! It's very cool believe me!
Maybe is possible have a version in objective.c or a demo project how to implement swift in objective.c?Many now use swift but I'm not so quick to learn😩

Thanks!

SwiftUI - cellForRowAtIndexPath never called in UIViewRepresentable

I'm trying to use this library in SwiftUI but there seems to be an issue. When I present the picker, nothing is shown. I set breakpoint and cellForRowAtIndexPath is never called.

I searched online and another person is having the same exact issue: https://stackoverflow.com/questions/57297895/swiftui-uitableview-cellforrowatindexpath-never-called-in-uiviewrepresentable

Here is the code in StackOverflow that is very similar to mine that doesn't work:

struct FruitPicker: UIViewRepresentable {
    @Binding var fruits: [Fruit]
    @Binding var selectedFruit: Fruit

    class Coordinator: NSObject, PickerViewDelegate, PickerViewDataSource {
        @Binding var fruits: [Fruit]
        @Binding var selectedFruit: Fruit

        init(fruits: Binding<[Fruit]>, selectedFruit: Binding<Fruit>) {
            self._fruits = fruits
            self._selectedFruit = selectedFruit
        }

        func pickerViewNumberOfRows(_ pickerView: PickerView) -> Int {
            return self.fruits.count
        }

        func pickerViewHeightForRows(_ pickerView: PickerView) -> CGFloat {
            return 50
        }

        func pickerView(_ pickerView: PickerView, titleForRow row: Int) -> String {
            return self.fruits[row].name
        }

        func pickerView(_ pickerView: PickerView, didSelectRow row: Int) {
            self.selectedFruit = self.fruits[row]
        }
    }

    func makeCoordinator() -> FruitPicker.Coordinator {
        return Coordinator(fruits: self.$fruits, selectedFruit: self.$selectedFruit)
    }

    func makeUIView(context: UIViewRepresentableContext<FruitPicker>) -> PickerView {
        let pickerView = PickerView()

        pickerView.delegate = context.coordinator
        pickerView.dataSource = context.coordinator

        return pickerView
    }

    func updateUIView(_ pickerView: PickerView, context: UIViewRepresentableContext<FruitPicker>) {

    }
}

Could you please help out with this?

Fix in documentation

Small fix in documentation :
"And the title for each row:
func pickerView(pickerView: PickerView, titleForRow row: Int, index: Int) -> String {
let item = itemsThatYouWantToPresent[row]
return item.text
} "
But we need to use
let item = itemsThatYouWantToPresent[index], not row. It is correct in Example project but not in documentation.

Stuck in endless while loop, 0 rows for datasource.

In the PickerView.swift, I am in an endless loop here, which locks up my app. I'm returning 0 for the pickerViewNumberOfRows delegate, because the user has not yet entered any data. I set the delegate and datasource in viewDidLoad().

`fileprivate func selectedNearbyToMiddleRow(_ row: Int) {
currentSelectedRow = row
tableView.reloadData()

    repeat {
        // This line adjust the contentInset to UIEdgeInsetZero because when the PickerView are inside of a UIViewController 
        // presented by a UINavigation controller, the tableView contentInset is affected.
        tableView.contentInset = UIEdgeInsets.zero
        
        let indexOfSelectedRow = visibleIndexOfSelectedRow()
        tableView.setContentOffset(CGPoint(x: 0.0, y: CGFloat(indexOfSelectedRow) * rowHeight), animated: false)
        
        delegate?.pickerView?(self, didSelectRow: currentSelectedRow, index: currentSelectedIndex)
        
    } while !(numberOfRowsByDataSource > 0 && tableView.numberOfRows(inSection: 0) > 0)
}`

[BUG] The default selected row is not the middle one.

Hi,
thank you so much for this, it's really helpful!
I noticed that the default selected row is not the middle one.
I think the problem is in this code:

Int(ceil(Float(numberOfRowsByDataSource) / 2.0))

Since the row is an index, you have to subtract 1 to numberOfRowsByDataSource, to get the middle one.
For example if we have 5 items, ceil(2.5) is 3, but the middle item index is actually 2.

It would be great to have a public function (or delegate function) that lets you set the first selected item.

Thanks

[QUESTION] Errors with version 0.4.0

After updating my project to the 0.4.0 version I get the following errors after cleaning build folder and building:

  1. header "PickerView-Swift.h" not found. This file name is not referenced in my project. Also indicates: Could not build Objective-C module 'PickerView'. Which seems to be related to the header file not found. Note that my project is Swift 4.2 not Objc, except for one pod which is objc.

  2. Showing All Messages
    Pods/PickerView/Pod/Classes/PickerView.swift:397:76: 'orientationDidChangeNotification' has been renamed to 'NSNotification.Name.UIDeviceOrientationDidChange'

  3. Pods/PickerView/Pod/Classes/PickerView.swift:399:76: 'orientationDidChangeNotification' has been renamed to 'NSNotification.Name.UIDeviceOrientationDidChange'

Xcode Version 10.0 (10A254a).
pod version 1.5.3

Update all pods
Analyzing dependencies
Downloading dependencies
Installing AMPopTip 3.5.0 (was 3.4.0 and source changed to https://github.com/CocoaPods/Specs.git from https://github.com/cocoapods/specs.git)
Using CSV.swift (2.2.1)
Installing DeviceGuru 5.0.0 (was 4.0.4 and source changed to https://github.com/CocoaPods/Specs.git from https://github.com/cocoapods/specs.git)
Installing Google-Mobile-Ads-SDK 7.35.1 (was 7.33.1 and source changed to https://github.com/CocoaPods/Specs.git from https://github.com/cocoapods/specs.git)
Using ObjcExceptionBridging (1.0.1)
Installing PickerView 0.4.0 (was 0.3.4 and source changed to https://github.com/CocoaPods/Specs.git from https://github.com/cocoapods/specs.git)
Using SVProgressHUD (2.2.5)
Using SwiftyStoreKit (0.13.3)
Using TPInAppReceipt (1.2.1)
Using XCGLogger (6.1.0)
Generating Pods project
Integrating client projects
Sending stats
Pod installation complete! There are 9 dependencies from the Podfile and 10 total pods installed.

Infinite List on iOS 11

On iOS 11, an infinite list does not show the selected item anymore, and pressing on items does a really long scroll to the item.

This happens even on the example project included.

[FEATURE] Animated item selection

I would like to animate highlighting in styleForLabel, so i've overrides this method like this:
if highlighted { UIView.animate(withDuration: 0.5, animations: { label.transform = CGAffineTransform(scaleX: 1, y: 1) }) } else { UIView.animate(withDuration: 0.5, animations: { label.transform = CGAffineTransform(scaleX: 1/2.5, y: 1/2.5) }) }
But this gives me negative effect - it tries to animate labels before the view appears...
So i decided to give my VC an extra property - var beginAnimating = false, and set this to true in viewDidApear. Unfortunately this is not working as I expect =(
Please give me some advices with this issue. Thanks!

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.