Skip to content

hectormatos2011/iOSContextualMenu

Repository files navigation

iOSContextualMenu

Here lies an easy and awesome way to create contextual menus in iOS. Do you want to access a quick and beautiful popup menu by tapping on a sprite for your game? Do you want to do what Pinterest did and have a contextual menu popup in your UICollectionView by long pressing on a cell?

Well, you've come to the right place. Hopefully this will help my fellow iOS Developers create beautiful UX for their applications quickly and easily!

Alt text      Alt text      Alt text

Installation

iOSContextualMenu can be included in your project through any of these methods:

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate iOSContextualMenu through CocoaPods, make sure the use_frameworks! line is included in your Podfile (iOSContextualMenu is written in Swift so it needs to be brought in as a framework). Make sure these lines are somewhere in your Podfile:

pod 'iOSContextualMenu'

Then, run the following command in Terminal where the Podfile is located:

$ pod install

Afterwards, whenever you need iOSContextualMenu, add this line to the top of the file it's being used in:

import iOSContextualMenu

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 iOSContextualMenu into your Xcode project using Carthage, specify it in your Cartfile:

github "hectormatos2011/iOSContextualMenu"

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

Afterwards, whenever you need iOSContextualMenu, add this line to the top of the file it's being used in:

import iOSContextualMenu

Getting Started

To run the example project; clone the repo, and run pod install from the Example directory first. A full blown example is included there. If you don't want to do that, then read ahead!

Overview

The iOSContextualMenu uses a similar dataSource/delegate paradigm like UITableView, UICollectionView, UIPickerView, etc.

To implement in your code, follow these steps (This code in these steps is for putting a contextual menu in every cell of a UICollectionView. In this example, I'm adding the contextual menu code inside of my own UICollectionViewCell subclass. Feel free to add a contextual menu to any UIView or a subclass of UIView if this isn't what you want. The following example will also work for UITableView and its UITableViewCell subclasses):

Step 1

In your interface declaration, make sure to make your object comply to ContextualMenuDelegate and ContextualMenuDataSource

class CollectionViewCell: ContextualMenuDelegate, ContextualMenuDataSource {}

Step 2

After creating the UIView that you want to attach a contextual menu to, treat iOSContextualMenu as you would any UITableView

let contexualMenu: ContextualMenu()
contextualMenu.delegate = self
contextualMenu.dataSource = self
view.addSubview(contextualMenu)

Step 3

Now implement the contextual menu's dataSource methods like you would for any UITableView, UICollectionView, or UIPickerView

Data Source:

func numberOfMenuItems(for menu: ContextualMenu) -> Int {
    return shareItems.count
}

Step 4

After implementing your data source, we'll implement our delegate methods

func contextualMenu(_ menu: ContextualMenu, viewForMenuItemAt index: Int) -> UIView {
    return UIImageView(image: shareItems[index].image)
}

Requirements

Currently, the minimum iOS requirement is iOS9 for this class to work.

Author

Hector Matos, [email protected]

License

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