DropdownButton
示例
要运行示例项目,首先克隆仓库,然后从 Example 目录中运行 pod install
要求
安装
DropdownButton 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod 'DropdownButton'
基本用法
// Defining dropdown button like a UIbutton and configure it
let dropDownButton: DropdownButton = {
let button: DropdownButton = DropdownButton()
button.layer.cornerRadius = 10
button.backgroundColor = .green
button.setTitleColor(.white, for: .normal)
button.selectedRowColor = .lightGray
button.selectedRowTextColor = .white
button.rowHeight = 40
return button
}()
// The the Closure returns Selected Index and String
dropDownButton.didSelect { (selectedText , index ,id) in
self.valueLabel.text = "Selected String: \(selectedText) \n index: \(index)"
}
// seting options
let options = ["Option 1", "Option 2", "Option 3"]
let ids = [0, 1, 2]
dropDownButton.setOptions(options: options, optionIds: ids, selectedIndex: 0)
其他选项
操作
dropDownButton.expandList() // To show the Drop Down Menu
dropDownButton.collapseList() // To hide the Drop Down Menu
关闭
listWillAppear() {
//You can Do anything when Dropdown menu willAppear
}
listDidAppear() {
//You can Do anything when Dropdown menu listDidAppear
}
listWillDisappear() {
//You can Do anything when Dropdown menu listWillDisappear
}
listDidDisappear() {
//You can Do anything when Dropdown menu listDidDisappear
}
自定义下拉按钮
您可以自定义下拉列表的以下属性
hideOptionsWhenSelect
:当点击一个选项时隐藏列表的选项。默认值为true
selectedRowColor
:下拉列表中选中行的颜色,默认值是.cyan
rowBackgroundColor
:下拉列表的背景颜色,默认值是.white
listHeight
:列表的最大高度。默认值是150
rowHeight
:列表中行的默认高度。默认值是30
selectedIndex
:用于预先选择列表中的任何项
备注
1.如果在单个ViewController中使用多个下拉列表,必须在StoryBoard或 addSubview() 中保持反向顺序,以避免重叠
作者
Alexander Korus,[email protected],基于jriosdev的iOSDropDown库。
许可证
DropdownButton 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。