A Material Design drop down for iOS written in Swift.
Do pod try DropDown
in your console and run the project to try a demo.
To install CocoaPods, run sudo gem install cocoapods
in your console.
Use CocoaPods.
- Add
pod 'DropDown'
to your Podfile. - Install the pod(s) by running
pod install
. - Add
import DropDown
in the .swift files where you want to use it
Use Carthage.
- Create a file name
Cartfile
. - Add the line
github "AssistoLab/DropDown"
. - Run
carthage update
. - Drag the built
DropDown.framework
into your Xcode project.
- Download the latest code version or add the repository as a git submodule to your git-tracked project.
- Drag and drop the src, helpers and also the resources directory from the archive in your project navigator. Make sure to select Copy items when asked if you extracted the code archive outside of your project.
let dropDown = DropDown()
// The view to which the drop down will appear on
dropDown.anchorView = view // UIView or UIBarButtonItem
// The list of items to display. Can be changed dynamically
dropDown.dataSource = ["Car", "Motorcycle", "Truck"]
Optional properties:
// Action triggered on selection
dropDown.selectionAction = { [unowned self] (index: Int, item: String) in
print("Selected item: \(item) at index: \(index)")
}
// Will set a custom width instead of the anchor view width
dropDownLeft.width = 200
Display actions:
dropDown.show()
dropDown.hide()
Don't forget to put:
DropDown.startListeningToKeyboard()
in your AppDelegate
's didFinishLaunching
method so that the drop down will handle its display with the keyboard displayed even the first time a drop down is showed.
The drop down can be shown below or above the anchor view with:
dropDown.direction = .Any
With .Any
the drop down will try to displa itself below the anchor view when possible, otherwise above if there is more place than below.
You can restrict the possible directions by using .Top
or .Bottom
.
By default, the drop down will be shown onto to anchor view. It will hide it.
If you need the drop down to be below your anchor view when the direction of the drop down is .Bottom
, you can precise an offset like this:
// Top of drop down will be below the anchorView
dropDown.bottomOffset = CGPoint(x: 0, y:dropDown.anchorView.bounds.height)
If you set the drop down direction to .Any
or .Top
you can also precise the offset when the drop down will shown above like this:
// When drop down is displayed with `Direction.Top`, it will be above the anchorView
dropDown.topOffset = CGPoint(x: 0, y:-dropDown.anchorView.bounds.height)
Note the minus sign used here to offset to the top.
By default, the cells in the drop down have the dataSource
values as text.
If you want a custom formatted text for the cells, you can set cellConfiguration
like this:
dropDown.cellConfiguration = { [unowned self] (index, item) in
return "- \(item) (option \(index))"
}
You can also create your own custom cell, from your .xib file. To have something like this for example:
For this you have to:
- Create your custom xib
- Create a
DropDownCell
subclass - Link the cell in your xib to your custom class
- At least have a label in your nib to link to the
optionLabel
IBOutlet
in code - You're all set!
- From a
DropDown
instance, do:
dropDown.cellNib = UINib(nibName: "YourNib", bundle: nil)
dropDown.customCellConfiguration = { (index: Index, item: String, cell: DropDownCell) -> Void in
guard let cell = cell as? MyCell else { return }
// Setup your custom UI components
cell.flagImage = flagImages[index]
}
- And you're good to go! π
dropDown.cancelAction = { [unowned self] in
println("Drop down dismissed")
}
dropDown.willShowAction = { [unowned self] in
println("Drop down will show")
}
dropDown.dismissMode = .OnTap
You have 3 dismiss mode with the DismissMode
enum:
OnTap
: A tap oustide the drop down is needed to dismiss it (Default)Automatic
: No tap is needed to dismiss the drop down. As soon as the user interact with anything else than the drop down, the drop down is dismissedManual
: The drop down can only be dismissed manually (in code)
You can manually (pre)select a row with:
dropDown.selectRowAtIndex(3)
The data source is reloaded automatically when changing the dataSource
property.
If needed, you can reload the data source manually by doing:
dropDown.reloadAllComponents()
You can get info about the selected item at any time with this:
dropDown.selectedItem() // -> String?
dropDown.indexForSelectedRow() // -> Int?
You can customize these properties of the drop down:
textFont
: the font of the text for each cells of the drop down.textColor
: the color of the text for each cells of the drop down.backgroundColor
: the background color of the drop down.selectionBackgroundColor
: the background color of the selected cell in the drop down.cellHeight
: the height of the drop down cells.
You can change them through each instance of DropDown
or via UIAppearance
like this for example:
DropDown.appearance().textColor = UIColor.blackColor()
DropDown.appearance().textFont = UIFont.systemFontOfSize(15)
DropDown.appearance().backgroundColor = UIColor.whiteColor()
DropDown.appearance().selectionBackgroundColor = UIColor.lightGrayColor()
DropDown.appearance().cellHeight = 60
when calling the show
method, it returns a tuple like this:
(canBeDisplayed: Bool, offscreenHeight: CGFloat?)
canBeDisplayed
: Tells if there is enough height to display the drop down. If its value isfalse
, the drop down is not showed.offscreenHeight
: If the drop down was not able to show all cells from the data source at once,offscreenHeight
will contain the height needed to display all cells at once (without having to scroll through them). This can be used in a scroll view or table view to scroll enough before showing the drop down.
- Xcode 7+
- iOS 8+
- ARC
This project is under MIT license. For more information, see LICENSE
file.
DropDown was inspired by the Material Design version of the Simple Menu.
DropDown was done to integrate in a project I work on:
It will be updated when necessary and fixes will be done as soon as discovered to keep it up to date.
You can find me on Twitter @kevinh6113.
Enjoy!