Skip to content

Instantly share code, notes, and snippets.

@KalpeshTalkar
Created March 31, 2017 07:47
Show Gist options
  • Save KalpeshTalkar/f003ba959baa732e48aa7c8fc33ea8b4 to your computer and use it in GitHub Desktop.
Save KalpeshTalkar/f003ba959baa732e48aa7c8fc33ea8b4 to your computer and use it in GitHub Desktop.
Popup menu written in swift 2.2
//
// KPopupMenu.swift
//
// Created by Kalpesh on 30/03/17.
// Copyright © 2017 Kalpesh Talkar. All rights reserved.
//
import UIKit
protocol KPopupMenuDelegate {
func didselectMenuOption(menu: KPopupMenu, menuIndex: Int)
}
// Completeion Closure
typealias MenuOptionSelected = ((menu: KPopupMenu, optionIndex: Int) -> (Void))?
class KPopupMenu: UIViewController, UITableViewDataSource, UITableViewDelegate, UIPopoverPresentationControllerDelegate {
// Tableview to display the menu options
@IBOutlet weak var menuTable: UITableView!
// Menu menu options
var menuItems = [AnyObject]()
// Delegate
var delegate: KPopupMenuDelegate?
// Completion closure/block
var onMenuOPtionSelect: MenuOptionSelected?
// Separator color
var separatorColor: UIColor? = UIColor.clearColor()
// Source: the menu is presented from this source
private var barButtonItem: UIBarButtonItem?
// Cell identifier
private let CELL_ID = "menuCell"
// MARK: - init methods
init(menuOptions: [AnyObject], source: UIBarButtonItem, delegate: KPopupMenuDelegate?) {
super.init(nibName: String(KPopupMenu), bundle: nil)
initialSetup(menuOptions, source: source)
self.delegate = delegate
}
init(menuOptions: [AnyObject], source: UIBarButtonItem, onOptionSelect: MenuOptionSelected) {
super.init(nibName: String(KPopupMenu), bundle: nil)
initialSetup(menuOptions, source: source)
onMenuOPtionSelect = onOptionSelect
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
// Initial setup
func initialSetup(menuOptions: [AnyObject], source: UIBarButtonItem) {
menuItems = menuOptions
barButtonItem = source
self.preferredContentSize = CGSizeMake(150, 130)
self.modalPresentationStyle = UIModalPresentationStyle.Popover
self.popoverPresentationController!.delegate = self
}
// MARK: - Life cycle methods
override func viewDidLoad() {
super.viewDidLoad()
setUpMenuTable()
}
// MARK: - UITableView setup
func setUpMenuTable() {
menuTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: CELL_ID)
if nil != separatorColor {
menuTable.separatorColor = separatorColor!
}
menuTable.separatorInset = UIEdgeInsetsZero
menuTable.dataSource = self
menuTable.delegate = self
menuTable.reloadData()
}
// MARK: - UITableViewDataSource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menuItems.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(CELL_ID, forIndexPath: indexPath)
cell.textLabel?.font = UIConstants.Fonts.MediumLight
cell.textLabel?.textColor = UIConstants.Colors.DarkGrayPrimary
cell.textLabel?.text = menuItems[indexPath.row].description
return cell
}
// MARK: - UITableViewDelegate
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Call the delegate method/completion block here and dismiss the controller
if nil != delegate {
delegate!.didselectMenuOption(self, menuIndex: indexPath.row)
} else if nil != onMenuOPtionSelect {
onMenuOPtionSelect!!(menu: self, optionIndex: indexPath.row)
}
dismissViewControllerAnimated(true, completion: nil)
}
// MARK: - UIPopoverPresentationControllerDelegate
func prepareForPopoverPresentation(popoverPresentationController: UIPopoverPresentationController) {
popoverPresentationController.permittedArrowDirections = .Any
popoverPresentationController.barButtonItem = barButtonItem
}
// MARK: - UIAdaptivePresentationControllerDelegate
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.None
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment