Skip to content

Instantly share code, notes, and snippets.

@cathandnya
Created February 2, 2016 12:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cathandnya/0ed0c2369aed9aa039af to your computer and use it in GitHub Desktop.
Save cathandnya/0ed0c2369aed9aa039af to your computer and use it in GitHub Desktop.
PopoverActionViewController.swift
//
// PopoverActionViewController.swift
// ORE2
//
// Created by nya on 2/2/16.
// Copyright © 2016 cathand.org. All rights reserved.
//
import UIKit
class PopoverAction {
var title: String
var handler: () -> Void
init(title: String, handler: () -> Void) {
self.title = title
self.handler = handler
}
}
class PopoverActionViewController: UITableViewController, UIPopoverPresentationControllerDelegate {
class func show(viewController: UIViewController, from: UIView, actions: [PopoverAction]) {
let vc = PopoverActionViewController()
vc.actions = actions
vc.modalPresentationStyle = .Popover
if let presentationController = vc.popoverPresentationController {
presentationController.permittedArrowDirections = [.Up, .Down]
presentationController.sourceView = from
presentationController.sourceRect = from.bounds
presentationController.delegate = vc
}
viewController.presentViewController(vc, animated: true, completion: nil);
}
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return .None
}
var actions = [PopoverAction]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = 44
tableView.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15)
tableView.layoutMargins = UIEdgeInsetsZero
tableView.backgroundColor = UIColor.clearColor()
}
override var preferredContentSize: CGSize {
get {
return CGSizeMake(300, CGFloat(actions.count) * tableView.rowHeight - 0.5)
}
set(val) {
super.preferredContentSize = val
}
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return actions.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("cell")
if cell == nil {
cell = UITableViewCell(style: .Default, reuseIdentifier: "cell")
cell.textLabel?.textAlignment = .Center
cell.backgroundColor = UIColor.clearColor()
cell.contentView.backgroundColor = UIColor.clearColor()
}
cell.textLabel?.text = actions[indexPath.row].title
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
dismissViewControllerAnimated(true) {
self.actions[indexPath.row].handler()
}
}
}
Copy link

ghost commented Oct 20, 2016

A curious question... from where and how are you calling this class?
I also gave it a star! :-) Thanks. --mike

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment