Swift library for displaying a modal sheet with a picker in iOS apps.
- đź“… Picker date
- đź•‘ Picker time
-
↕️ Picker custom value -
↔️ Multi picker - ⚙️ Fine-tuning the picker and setting a custom style
The example application is the best way to see SwiftModalPicker
in action. Simply open the SwiftModalPicker.xcodeproj
and run the Example
scheme.
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
.
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.
let picker = SwiftModalPicker(type: <SwiftModalPicker.PickerType>, toolbarItems: <[SwiftModalPicker.ToolbarButton]>)
pickerViewPresenter.showPicker(from: self.view)
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)?
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
]
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
]
let pickerViewPresenter = SwiftModalPicker(type: .calendar(params: SwiftModalPicker.CalendarParams(selectedDate: Date(), minimumDate: nil, maximumDate: nil, datePickerMode: .date, timeZone: .current)), toolbarItems: pickerToolbarButtons)
pickerViewPresenter.onDatePickerDone = { date in
...
}
let pickerViewPresenter = SwiftModalPicker(type: .calendar(params: SwiftModalPicker.CalendarParams(selectedDate: Date(), minimumDate: nil, maximumDate: nil, datePickerMode: .time, timeZone: .current)), toolbarItems: pickerToolbarButtons)
pickerViewPresenter.onDatePickerDone = { date in
...
}
let items = ["Apple", "Avocado", "Banana", "Blackberries"]
let pickerViewPresenter = SwiftModalPicker(type: .custom(items: items, selectedIndex: 2), toolbarItems: pickerToolbarButtons)
pickerViewPresenter.onPickerDone = { (val, index) in
...
}
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
...
}
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)
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
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.