A more convenient way to display a popover with a built-in picker, on iPhone/iPad of iOS9+.
- By simple code, you can display a popover that contains a built-in picker, on iPhone or iPad.
- Swift 5, iOS9+. UIPopoverController free.
- Callback
DatePickerPopover(title: "DatePicker")
.setDoneButton(action: { _, selectedDate in print(selectedDate)})
.appear(originView: sender, baseViewController: self)
- Swift 5, Xcode 11.
- iOS 9+
- CocoaPods 1.1.0.rc.2+ or Carthage 0.12.0+
MIT
Specify it in your 'Podfile', after replacing ‘YourProjectTargetName’ with your own target name:
platform :ios, '9.0'
use_frameworks!
target ‘YourProjectTargetName’ do
pod 'SwiftyPickerPopover'
end
Run 'pod install'.
- Add it to your Cartfile:
github "hsylife/SwiftyPickerPopover"
- Run
carthage update --platform iOS
- Add 'SwiftyPickerPopover.framework' to 'Linked Frameworks and Library' on your project.
- Add
/usr/local/bin/carthage copy-frameworks
to 'New Run Script Phase'. - Add
$(SRCROOT)/Carthage/Build/iOS/SwiftyPickerPopover.framework
to 'Input Files'.
On your .swift file, import the module:
import SwiftyPickerPopover
SwiftyPickerPopover offers you the following popovers:
- StringPickerPopover: has an UIPickerView that allows user to choose a String type choice.
- ColumnStringPickerPopover: has an UIPickerView of multiple columns.
- DatePickerPopover: has an UIDatePicker that allows user to choose a Date type choice.
- CountdownPickerPopover: has an UIDatePicker specializing in countDownTimer style.
All popovers have the following APIs.
-
setPermittedArrowDirections()
-
setArrowColor()
-
setSize(width:,height:)
-
setCornerRadius()
-
setValueChange(action:)
-
setOutsideTapDismissing(allowed:)
-
setDimmedBackgroundView(enabled:)
-
appear(originView:, baseViewWhenOriginViewHasNoSuperview:, baseViewController:, completion:)
-
appear(barButtonItem:, baseViewWhenOriginViewHasNoSuperview:, baseViewController:, completion:)
-
disappear()
-
disappearAutomatically(after seconds:, completion:)
-
reload()
-
init(title:, choices:)
-
setFont()
-
setFontSize()
-
setFontColor()
-
setImageNames()
-
setImages()
-
setSelectedRow()
-
setRowHeight()
-
setDisplayStringFor()
-
setDoneButton(title:, font:, color:, action:)
-
setCancelButton(title:, font:, color:, action:)
-
setClearButton(title:, font:, color:, action:)
StringPickerPopover(title: "StringPicker", choices: ["value 1","value 2","value 3"])
.setSelectedRow(0)
.setValueChange(action: { _, selectedDate in
print("current date \(selectedDate)")
})
.setDoneButton(action: { (popover, selectedRow, selectedString) in
print("done row \(selectedRow) \(selectedString)")
})
.setCancelButton(action: { (_, _, _) in print("cancel")}
)
.appear(originView: button, baseViewController: self)
After adding image files to your target's Assets.xcassets:
StringPickerPopover(title: "StringPicker", choices: ["value 1","value2",""])
.setImageNames(["Icon1",nil,"Icon3"])
.appear(originView: button, baseViewController: self)
let displayStringFor:((String?)->String?)? = { string in
if let s = string {
switch(s){
case "value 1":
return "😊"
case "value 2":
return "😏"
case "value 3":
return "😓"
default:
return s
}
}
return nil
}
let p = StringPickerPopover(title: "StringPicker", choices: ["value 1","value 2","value 3"])
.setDisplayStringFor(displayStringFor)
.setDoneButton(action: {
popover, selectedRow, selectedString in
print("done row \(selectedRow) \(selectedString)")
})
.setCancelButton(action: { _, _, _ in
print("cancel")
})
p.appear(originView: sender, baseViewController: self)
p.disappearAutomatically(after: 3.0, completion: { print("automatically hidden")} )
StringPickerPopover(title: "Narrow StringPicker", choices: ["value 1","value 2","value 3"])
.setSize(width: 250.0)
.appear(originView: sender, baseViewController: self)
The default width and height of popover are both 300.0. By using setSize(width:, height:), you can override it or them. When you set nil to the parameter or don't specify it, the default will be used.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let theCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
let p = StringPickerPopover(title: "CollectionView", choices: ["value 1","value 2","value 3"])
.setSelectedRow(1)
.setDoneButton(title:"👌", action: { (popover, selectedRow, selectedString) in print("done row \(selectedRow) \(selectedString)") })
.setCancelButton(title:"🗑", action: { (_, _, _) in print("cancel")} )
p.appear(originView: theCell, baseViewWhenOriginViewHasNoSuperview collectionView, baseViewController: self)
}
If originView has no superView, then then you need to set baseViewWhenOriginViewHasNoSuperview as above to specify sourceView for the position for the arrow. If it has the superview, then SwiftyPickerPopover automatically use it for the sourceView.
let item: UIBarButtonItem = sender
let originView = item.value(forKey: "view") as! UIView
p.appear(originView: originView, baseViewController: self)
-
init(title:, choices:, selectedRows:, columnPercents:)
-
setFonts()
-
setFontSizes()
-
setFontColors()
-
setSelectedRows()
-
setDisplayStringFor()
-
setDoneButton(title:, font:, color:, action:)
-
setCancelButton(title:, font:, color:, action:)
-
setClearButton(title:, font:, color:, action:)
ColumnStringPickerPopover(title: "Columns Strings",
choices: [["Breakfast", "Lunch", "Dinner"],["Tacos", "Sushi", "Steak", "Waffles", "Burgers"]],
selectedRows: [0,0], columnPercents: [0.5, 0.5])
.setDoneButton(action: { popover, selectedRows, selectedStrings in print("selected rows \(selectedRows) strings \(selectedStrings)")})
.setCancelButton(action: {_, _, _ in print("cancel")})
.setFontSize(14)
.appear(originView: sender, baseViewController: self)
)
-
init(title:)
-
setSelectedDate()
-
setDateMode()
-
setMinimumDate()
-
setMaximumDate()
-
setMinuteInterval()
-
setLocale()
-
setDoneButton(title:, font:, color:, action:)
-
setCancelButton(title:, font:, color:, action:)
-
setClearButton(title:, font:, color:, action:)
DatePickerPopover(title: "DatePicker")
.setDateMode(.date)
.setSelectedDate(Date())
.setDoneButton(action: { popover, selectedDate in print("selectedDate \(selectedDate)")})
.setCancelButton(action: { _, _ in print("cancel")})
.appear(originView: sender, baseViewController: self)
let p = DatePickerPopover(title: "Clearable DatePicker")
.setDoneButton(action: { popover, selectedDate in print("selectedDate \(selectedDate)")} )
.setCancelButton(action: { _, _ in print("cancel")})
.setClearButton(action: { popover, selectedDate in
print("clear")
//Rewind
popover.setSelectedDate(Date()).reload()
})
p.appear(originView: sender, baseViewController: self)
p.disappearAutomatically(after: 3.0)
DatePickerPopover(title: "DatePicker .time 5minInt.")
.setDateMode(.time)
.setMinuteInterval(5)
.setPermittedArrowDirections(.down)
.setDoneButton(action: { popover, selectedDate in print("selectedDate \(selectedDate)")} )
.setCancelButton(action: { _, _ in print("cancel")})
.appear(originView: sender, baseViewController: self)
)
-
init(title:)
-
setSelectedTimeInterval
-
setDoneButton(title:, font:, color:, action:)
-
setCancelButton(title:, font:, color:, action:)
-
setClearButton(title:, font:, color:, action:)
CountdownPickerPopover(title: "CountdownPicker")
.setSelectedTimeInterval(TimeInterval())
.setDoneButton(action: { popover, timeInterval in print("timeInterval \(timeInterval)")} )
.setCancelButton(action: { _, _ in print("cancel")})
.setClearButton(action: { popover, timeInterval in print("Clear")
popover.setSelectedTimeInterval(TimeInterval()).reload()
})
.appear(originView: sender, baseViewController: self)
When you prepare your customized Storyboard, it will be applied automatically.
- Find the original file of the popover's Storyboard, which you want to change. For example, 'CountdownPickerPopover.storyboard'.
- Add it to your project on Xcode for including it into mainBundle. At this time, please check 'Copy items if needed'. Do not change the file name.
- Next, change the module specified by default. Open the Storyboard file on Xcode and uncheck 'Inherit From Target' in InterfaceBuilder > Identity inspector > Custom Class. Specify 'SwiftyPickerPopover' for 'Module'.
- Finally, customize your storyboard file.
- Ken Torimaru GitHub for CountdownPickerPopover and ColumnStringPickerPopover.
- BalestraPatrick GitHub for README.md typo.
- andersonlucasg3 GitHub for adding possibility to override the storyboards with custom localizations in the app project.
- lswith GitHub for fixing circular reference issue of Cartfile.
- coybit GitHub for adding setImages() to StringPickerPopover.
- Mihael Isaev GitHub for adding appear() from barButtonItem.
- iosMaher GitHub for idea of setFont() and setFontColor().
- gbuela GitHub for setValueChange(action:) API for all popover types.
- ikbalyasar GitHub for demo code for showing selected value on StringPickerPopover.
- weakfl GitHub for update to Swift 4.2
- Tobisaninfo[GitHub](https://github.com/Tobisaninfo( for cartage support with Xcode 10.2
- AlexwingGitHub for update to Swift 5.0