Created
February 2, 2016 12:32
-
-
Save cathandnya/0ed0c2369aed9aa039af to your computer and use it in GitHub Desktop.
PopoverActionViewController.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A curious question... from where and how are you calling this class?
I also gave it a star! :-) Thanks. --mike