Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- Represents the overflow menu view.
- */
- class PopoverView: UITableViewController {
- // MARK: - Constants
- private enum Constant {
- static let defaultCellRowHeight = 44
- static let seperatorLineHeight: CGFloat = 0.5
- }
- // MARK: - Public Properties
- weak var delegate: PopoverViewDelegate?
- // MARK: - Private Properties
- private var actionCells: [UITableViewCell]!
- // MARK: - Lifecycle
- static func create(on view: UIView, withItems items: [String], atPosition position: CGPoint, with popoverPresentationControllerDelegate: UIPopoverPresentationControllerDelegate, anchorView: UIView) -> PopoverView {
- let popOverView = UIStoryboard(name: "Popover", bundle: nil).instantiateInitialViewController() as! PopoverView
- popOverView.actionCells = createCells(withItems: items)
- let popOverHeight = CGFloat(popOverView.actionCells.count * Constant.defaultCellRowHeight) - Constant.seperatorLineHeight // Subracting separator lineheight avoids showing last seperator line.
- popOverView.preferredContentSize = CGSize(width: calculateMaxWidth(from: popOverView.actionCells), height: popOverHeight)
- popOverView.modalPresentationStyle = UIModalPresentationStyle.popover
- let popOver: UIPopoverPresentationController = popOverView.popoverPresentationController!
- // Set direction of the popover (should it be displayed below or under the trigger position)
- if (position.y + popOverHeight) > view.bounds.height {
- popOver.permittedArrowDirections = .down
- popOver.sourceRect = CGRect(x: anchorView.bounds.origin.x, y: anchorView.bounds.origin.y + 8, width: anchorView.bounds.width, height: anchorView.bounds.height)
- } else {
- popOver.permittedArrowDirections = .up
- popOver.sourceRect = CGRect(x: anchorView.bounds.origin.x, y: anchorView.bounds.origin.y - 8, width: anchorView.bounds.width, height: anchorView.bounds.height)
- }
- popOver.passthroughViews = [view]
- popOver.delegate = popoverPresentationControllerDelegate
- popOver.sourceView = anchorView
- popOver.backgroundColor = UIColor.white
- return popOverView
- }
- // MARK: - Private Methods
- private static func createCells(withItems items: [String]) -> [UITableViewCell] {
- var cells = [UITableViewCell]()
- items.forEach { item in
- let cell = UITableViewCell()
- cell.textLabel?.text = item
- cell.textLabel?.translatesAutoresizingMaskIntoConstraints = false
- cells.append(cell)
- }
- return cells
- }
- private static func calculateMaxWidth(from cells: [UITableViewCell]) -> CGFloat {
- var maxWidth: CGFloat = 0
- cells.forEach { cell in
- let calculatedWidth = cell.textLabel!.intrinsicContentSize.width + 32
- if maxWidth < calculatedWidth {
- maxWidth = calculatedWidth
- }
- }
- return maxWidth
- }
- // MARK: - Table view data source
- override func numberOfSections(in tableView: UITableView) -> Int {
- return 1
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return actionCells.count
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- return actionCells[indexPath.row]
- }
- override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- delegate?.popoverViewItemSelected(popoverTableViewController: self, atIndex: indexPath.row)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement