介绍
这个优雅的 Swift 下拉菜单位于导航栏下方,当用户点击导航标题时,它会显示相关物品列表。
安装
BTNavigationDropdownMenu 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中:
use_frameworks!
pod 'BTNavigationDropdownMenu'
继续在您的 Swift 文件中 导入 BTNavigationDropdownMenu
import BTNavigationDropdownMenu
注意:此库支持 Xcode 8.3 Swift 3.1 和嵌入式框架。
用法
实例化
首先创建一个包含字符串数组作为下拉菜单元素的 Array
let items = ["Most Popular", "Latest", "Trending", "Nearest", "Top Picks"]
创建一个新的 BTNavigationDropdownMenu 实例
let menuView = BTNavigationDropdownMenu(navigationController: self.navigationController, containerView: self.navigationController!.view, title: BTTitle.title("Dropdown Menu"), items: items)
或者这样
let menuView = BTNavigationDropdownMenu(title: BTTitle.index(1), items: items)
BTTitle 是一个枚举。我们可以设置 BTTitle.title(string)
或 BTTitle.index(Int)
(注意:BTTitle.index
是定义的 items
数组的索引)。
默认情况下,navigationController
是最顶层的导航控制器,而 containerView
是 keyWindow。
(建议将 keyWindow
用于 containerView
,以便黑色覆盖层可以覆盖整个屏幕。在某些情况下,keyWindow
可能无法正常工作,例如与侧边菜单(如 SWRevealViewController)一起使用时,下拉菜单不会随父视图或导航控制器一起移动。要解决此问题,您可以使用 self.navigationController!.view
。)
将 导航栏标题 设置为菜单视图
self.navigationItem.titleView = menuView
调用 BTNavigationDropdownMenu 闭包以获取 所选单元格的索引
menuView.didSelectItemAtIndexHandler = {[weak self] (indexPath: Int) -> () in
print("Did select item at index: \(indexPath)")
self.selectedCellLabel.text = items[indexPath]
}
使用 menuView.show()
或 menuView.hide()
手动显示或隐藏下拉菜单。
使用 menuView.toggle()
切换下拉菜单的显示/隐藏。
通过 menuView.isShown
(布尔类型) 属性检查下拉菜单的显示状态。
使用 menuView.updateItems(items: [AnyObject])
更新下拉菜单中的项目。
自定义
首先,分配下拉菜单的项目和框架。然后,通过覆盖这些属性来自定义菜单的外观和感觉
cellHeight
- 单元格高度 默认为 50
cellBackgroundColor
- 单元格背景颜色 默认为 whiteColor()
cellSeparatorColor
- 单元格分隔线颜色 默认为 darkGrayColor()
cellTextLabelColor
- 单元格内文本颜色 默认为 darkGrayColor()
cellTextLabelFont
- 单元格内字体 默认为 HelveticaNeue-Bold,大小 17
navigationBarTitleFont
- 导航栏标题字体 默认为 HelveticaNeue-Bold,大小 17
cellTextLabelAlignment
- 单元格内文本对齐方式 默认为 .Left
cellSelectionColor
- 单元格选中颜色 默认为 lightGrayColor()
checkMarkImage
- 单元格复选标记图标。
animationDuration
- 显示/隐藏菜单的动画时长 默认为 0.5s
arrowImage
- 导航标题旁边的箭头
arrowPadding
- 导航标题和箭头之间的填充 默认为 15
maskBackgroundColor
- 遮罩层颜色 默认为 blackColor()
maskBackgroundOpacity
- 遮罩层的不透明度 默认为 0.3
menuTitleColor
- 菜单中显示的标题颜色。 默认为 lightGrayColor()
shouldKeepSelectedCellColor
- 显示菜单时选中单元格颜色的可见性 默认为 false
shouldChangeTitleText
- 改变选中标题文本的能力 默认为 true
selectedCellTextLabelColor
- 选中单元格文本标签颜色 默认为 darkGrayColor()
arrowTintColor
- 箭头的着色颜色。 默认为 whiteColor()
需求
- iOS 8.0+(带有 Swift 支持的 CocoaPods 仅在 iOS 8.0+ 上工作。另请将库手动导入到您的项目中)
- Xcode 8.0+,Swift 3.0+
更改记录
详情请查阅变更记录
许可证
BTNavigationDropdownMenu 采用 MIT 许可证。详情请查阅许可证。