简介
这款优雅且实用的下拉菜单是用 Swift 编写的,位于导航栏下方,当用户点击导航标题时,会显示一系列定义好的条目。
安装
CocoaPods
BTNavigationDropdownMenu 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
use_frameworks!
pod 'BTNavigationDropdownMenu'
接下来,将 BTNavigationDropdownMenu 导入 到您自己的 Swift 文件中
import BTNavigationDropdownMenu
Carthage
Carthage 是一种去中心化的依赖管理器,它构建您的依赖项,并提供二进制框架。
您可以使用 Homebrew 通过以下命令安装 Carthage:
$ brew update
$ brew install carthage
要使用 Carthage 将 BTNavigationDropdownMenu 集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它
github "PhamBaTho/BTNavigationDropdownMenu" ~> 0.7
运行carthage update
来构建框架,并将生成的BTNavigationDropdownMenu.framework
拖入您的Xcode项目中。
使用
实例化
首先,创建一个包含字符串作为下拉列表元素的数组。
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
是键窗口。
(对于containerView
推荐使用keyWindow
,以便黑色遮罩覆盖整个屏幕。在某些情况下,如与侧菜单(例如SWRevealViewController)一起使用时,keyWindow
可能工作不正常,比如下拉菜单不会随父视图或导航控制器移动。为了解决这个问题,您可以使用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
- 单元内字体 默认是bold system font,大小17
navigationBarTitleFont
- 导航栏标题字体 默认是vold system font,大小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 9.0+
变更日志
查看变更日志以获取详细信息
许可
BTNavigationDropdownMenu 在 MIT 许可证下可用。查看许可以获取详细信息。