ASJDropDownMenu 1.2

ASJDropDownMenu 1.2

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Jan 2022

Maintained by Sudeep.



  • By
  • Sudeep

ASJDropDownMenu

iOS doesn't have a drop down menu by default. Developers are supposed to use the UIPickerView for similar functionality. Project design constraints may sometimes however necessitate the use of such a menu. This class is a subclass of UIView that has a UITableView embedded in it. It provides the functionality needed to show a drop down menu below any UIView and is customizable using various properties.

Installation

CocoaPods is the preferred way to install this library. Add this command to your Podfile:

pod 'ASJDropDownMenu'

Usage

Creating one is simple. The show method has a completion block which returns the selected item. You may hide the drop down there and then:

ASJDropDownMenu *dropDownMenu = [[ASJDropDownMenu alloc] initWithView:aView menuItems:anArrayOfASJDropDownMenuItems];
[dropDown showMenuWithCompletion:^(ASJDropDownMenu *dropDownMenu, ASJDropDownMenuItem *menuItem, NSUInteger index)
 {
   [dropDownMenu hideMenu];
 }];

Whichever view you provide during instantiation, the dropdown will appear exactly below it. The menu items need to be of type ASJDropDownMenuItems. Constructor methods are provided to generate them. Just attach an array of these to your drop down menu instance.

ASJDropDownMenuItem *itemWithTitle = [ASJDropDownMenuItem itemWithTitle:@"a title"];
ASJDropDownMenuItem *anotherItemWithTitle = [ASJDropDownMenuItem itemWithTitle:@"another title"];
anInstanceOfDropDownMenu.menuItems = @[itemWithTitle, anotherItemWithTitle];
ASJDropDownMenuItem *itemWithSubtitle = [ASJDropDownMenuItem itemWithTitle:@"a title" subtitle:@"a subtitle" image:anImage];
ASJDropDownMenuItem *anotherItemWithSubtitle = [ASJDropDownMenuItem itemWithTitle:@"another title" subtitle:@"another subtitle" image:anotherImage];
anInstanceOfDropDownMenu.menuItems = @[itemWithSubtitle, anotherItemWithSubitle];

alt tag

Customization

There are a few properties that you can use to customize the drop down menu:

@property (nullable, strong, nonatomic) UIColor *menuColor;

Sets the menu's background color.

@property (nullable, strong, nonatomic) UIColor *itemColor;

Sets the text color of the menu items.

@property (nullable, strong, nonatomic) UIFont *itemFont;

Sets the menu items' font.

@property (assign, nonatomic) CGFloat itemHeight;

Sets the height each menu item takes.

@property (assign, nonatomic) CGFloat animationDuration;

Sets the time taken (in seconds) to hide or show the menu. Defaults to 0.4.

@property (assign, nonatomic) BOOL hidesOnSelection;

If set YES, the menu will hide when an item is tapped.

@property (assign, nonatomic) ASJDropDownMenuDirection direction;

You can show the menu below or above the specified view. Shows below by default.

@property (assign, nonatomic) ASJDropDownMenuScrollIndicatorStyle indicatorStyle;

You can set the scroll indicator styles default, black or white, which are the default UIScrollView indicator styles. If you want to set a custom color to the scroll indicator, you can look up ASJColoredScrollIndicators.

To-do

  • Animation to open and close.
  • Provision to add images.
  • Provision to close on item selection.
  • Option to open the drop down in up or down direction.
  • Move the screen to accomodate the drawer when keyboard shows.

License

ASJDropDownMenu is available under the MIT license. See the LICENSE file for more info.