8000 GitHub - moslienko/SwiftModalPicker: Swift library for displaying a modal sheet with a picker in iOS apps
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

moslienko/SwiftModalPicker

Repository files navigation

SwiftModalPicker Logo

Swift 5.2 SPM

SwiftModalPicker

Swift library for displaying a modal sheet with a picker in iOS apps.

Features

  • đź“… Picker date
  • đź•‘ Picker time
  • ↕️ Picker custom value
  • ↔️ Multi picker
  • ⚙️ Fine-tuning the picker and setting a custom style

Example

The example application is the best way to see SwiftModalPicker in action. Simply open the SwiftModalPicker.xcodeproj and run the Example scheme.

Installation

Swift Package Manager

To integrate using Apple's Swift Package Manager, add the following as a dependency to your Package.swift:

dependencies: [
    .package(url: "https://github.com/moslienko/SwiftModalPicker.git", from: "1.0.0")
]

Alternatively navigate to your Xcode project, select Swift Packages and click the + icon to search for SwiftModalPicker.

Manually

If you prefer not to use any of the aforementioned dependency managers, you can integrate SwiftModalPicker into your project manually. Simply drag the Sources Folder into your Xcode project.

Usage

Create picker and show

	let picker = SwiftModalPicker(type: <SwiftModalPicker.PickerType>, toolbarItems: <[SwiftModalPicker.ToolbarButton]>)
	pickerViewPresenter.showPicker(from: self.view)

Handlers

    public var onDatePickerDone: ((_ date: Date) -> Void)?
    public var onPickerDone: ((_ value: String, _ index: Int) -> Void)?
    public var onPickerMiltiComponentsDone: ((_ values: [String], _ indexes: [Int]) -> Void)?
    public var onPickerClosed: (() -> Void)?

Toolbar

	public enum ToolbarButton {
        	case cancel(title: String)
        	case done
        	case custom(button: UIBarButtonItem)
        	case space
	}

Usage example

	let button = UIBarButtonItem(title: "Remove date", style: .plain, target: self, action: #selector(self.removeDate))
        let pickerToolbarButtons: [SwiftModalPicker.ToolbarButton] = [
            .cancel(title: "Cancel"),
            .custom(button: button),
            .space,
            .done
        ]

Picker title

You can specify your own value for the picker title using the UIBarButtonItem custom button for the toolbar

	let label = UILabel(frame: .zero)
        label.text = "Title for picker"
        label.textAlignment = .center
        label.textColor = .black
        label.font = UIFont.boldSystemFont(ofSize: 16.0)
        let pickerTitle = UIBarButtonItem(customView: label)
        
        let pickerToolbarButtons: [SwiftModalPicker.ToolbarButton] = [
            .cancel(title: "Cancel"),
            .space,
            .custom(button: pickerTitle),
            .space,
            .done
        ]

Date picker

	  let pickerViewPresenter = SwiftModalPicker(type: .calendar(params: SwiftModalPicker.CalendarParams(selectedDate: Date(), minimumDate: nil, maximumDate: nil, datePickerMode: .date, timeZone: .current)), toolbarItems: pickerToolbarButtons)
        
        pickerViewPresenter.onDatePickerDone = { date in
            ...
        }

Time picker

	 let pickerViewPresenter = SwiftModalPicker(type: .calendar(params: SwiftModalPicker.CalendarParams(selectedDate: Date(), minimumDate: nil, maximumDate: nil, datePickerMode: .time, timeZone: .current)), toolbarItems: pickerToolbarButtons)
        pickerViewPresenter.onDatePickerDone = { date in
            ...
        }

CustomPicker

	let items = ["Apple", "Avocado", "Banana", "Blackberries"]
        let pickerViewPresenter = SwiftModalPicker(type: .custom(items: items, selectedIndex: 2), toolbarItems: pickerToolbarButtons)
        pickerViewPresenter.onPickerDone = { (val, index) in
            ...
        }

MultiComponentPicker

	let items = [["iPhone", "iPad", "MacBook", "Mac mini"], ["AirPods", "AirPods Pro", "AirPods Max"]]
        let pickerViewPresenter = SwiftModalPicker(type: .customWithMultiRows(items: items, selectedIndexes: [2, 1]), toolbarItems: pickerToolbarButtons)
        
        pickerViewPresenter.onPickerMiltiComponentsDone = { (values, indexes) in
            ...
        }

Customization

The following options are available to change the picker style

public var viewTintColor: UIColor?
public var pickerBackgroundColor: UIColor?
public var pickerColor: UIColor?
public var toolbarBackgroundColor: UIColor?

Example

	let pickerViewPresenter = SwiftModalPicker(type: .calendar(params: SwiftModalPicker.CalendarParams(selectedDate: Date(), minimumDate: nil, maximumDate: nil, datePickerMode: .date, timeZone: .current)), toolbarItems: pickerToolbarButtons)
        pickerViewPresenter.viewTintColor = .red
        pickerViewPresenter.pickerBackgroundColor = .black
        pickerViewPresenter.pickerColor = .white
        pickerViewPresenter.toolbarBackgroundColor = UIColor.black.withAlphaComponent(0.7)

Get the current picker

let picker = view.getActiveSwiftModalPicker()

Further actions with him - you can close the picker

picker?.close()

You can get the current value

picker?.donePicker()

As a result of which onPickerDone event will be triggered

License

SwiftModalPicker
Copyright (c) 2021 Pavel Moslienko 8676976+moslienko@users.noreply.github.com

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.

About

Swift library for displaying a modal sheet with a picker in iOS apps

Resources

License

Stars

Watchers

Forks

Packages

No packages published
0