A more convenient way to display a popover with a built-in picker, on iPhone/iPad of iOS9+.
DatePickerPopover(title: "DatePicker")
.setDoneButton(action: { _, selectedDate in print(selectedDate)})
.appear(originView: sender, baseViewController: self)
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'.
github "hsylife/SwiftyPickerPopover"
carthage update --platform iOS
/usr/local/bin/carthage copy-frameworks
to 'New Run Script Phase'.$(SRCROOT)/Carthage/Build/iOS/SwiftyPickerPopover.framework
to 'Input Files'.On your .swift file, import the module:
import SwiftyPickerPopover
SwiftyPickerPopover offers you the following popovers:
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.