Home

Awesome

alt tag

Pod Version License: MIT Language

<!--[![Build Status](https://travis-ci.org/PhamBaTho/BTNavigationDropdownMenu.svg?branch=master)](https://travis-ci.org/PhamBaTho/BTNavigationDropdownMenu)-->

Introduction

The elegant yet functional dropdown menu, written in Swift, appears underneath the navigation bar to display a list of defined items when a user clicks on the navigation title.

alt tag

Installation

CocoaPods

BTNavigationDropdownMenu is available through CocoaPods. To install it, simply add the following line to your Podfile:

use_frameworks!
pod 'BTNavigationDropdownMenu'

Go ahead and import BTNavigationDropdownMenu into your own Swift files

import BTNavigationDropdownMenu

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate BTNavigationDropdownMenu into your Xcode project using Carthage, specify it in your Cartfile:

github "PhamBaTho/BTNavigationDropdownMenu" ~> 0.7

Run carthage update to build the framework and drag the built BTNavigationDropdownMenu.framework into your Xcode project.

Usage

Instantiate

Start by creating an Array that contains strings as elements of a dropdown list:

let items = ["Most Popular", "Latest", "Trending", "Nearest", "Top Picks"]

Create a new instance of BTNavigationDropdownMenu:

let menuView = BTNavigationDropdownMenu(navigationController: self.navigationController, containerView: self.navigationController!.view, title: BTTitle.title("Dropdown Menu"), items: items)

or like this:

let menuView = BTNavigationDropdownMenu(title: BTTitle.index(1), items: items)

BTTitle is an enum. We can set BTTitle.title(string) or BTTitle.index(Int) (Note: BTTitle.index is the index of defined items array).

By default, navigationController is the topmost navigation controller and containerView is keyWindow.

(keyWindow is recommended for containerView so that the black overlay can cover the whole screen. In some cases, keyWindow doesn't work properly, like using with side menu (e.g. SWRevealViewController), the dropdown menu doesn't move along with the parent view or navigation controller. To workaround this issue, you can use self.navigationController!.view.)

Set title of navigation bar as menuView:

self.navigationItem.titleView = menuView

Call BTNavigationDropdownMenu closure to get the index of selected cell:

menuView.didSelectItemAtIndexHandler = {[weak self] (indexPath: Int) -> () in
            print("Did select item at index: \(indexPath)")
            self.selectedCellLabel.text = items[indexPath]
}

menuView.show() or menuView.hide() to show or hide dropdown menu manually.

menuView.toggle() to toogle dropdown menu shown/hide.

menuView.isShown (Boolean type) property to check showing state of dropdown menu.

menuView.updateItems(items: [AnyObject]) to update items in dropdown menu.

Customization

First, assign the items and frame for the dropdown menu. Then, customize the look and feel of the menu by overriding these properties:

NameDescriptionDefault value
navigationBarTitleFontNavigation bar title fontSystem font, bold, size 17
cellHeightCell height50
arrowImageArrow next to the navigation titleProvided image
arrowPaddingPadding between the navigation title and arrow15
cellBackgroundColorCell background colorUIColor.white
cellSeparatorColorCell separator colorUIColor.darkGray
cellTextLabelColorText color inside of the cellUIColor.darkGray
cellTextLabelFontText font inside the cellSystem font, bold, size 17
cellTextLabelAlignmentText alignment inside of the cellNSTextAlignment.left
cellSelectionColorSelected color of the cellUIColor.lightGray
checkMarkImageCheckmark icon for the cellProvided image
animationDurationAnimation duration for showing/hiding of the menu0.3
maskBackgroundColorMask layer colorUIColor.black
maskBackgroundOpacityOpacity of the mask layer0.3
menuTitleColorTitle color displayed in the menuUIColor.lightGray
shouldKeepSelectedCellColorSelected cell color visibility when menu is shownfalse
shouldChangeTitleTextAbility to change the selected title texttrue
selectedCellTextLabelColorSelected cell text label colorUIColor.darkGray
arrowTintColorTint color of the arrowUIColor.white

Requirement

Changelog

See the CHANGELOG for details

License

BTNavigationDropdownMenu is available under the MIT License. See the LICENSE for details.