Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.76 KB | None | 0 0
  1. /**
  2.  Represents the overflow menu view.
  3.  */
  4. class PopoverView: UITableViewController {
  5.    
  6.     // MARK: - Constants
  7.    
  8.     private enum Constant {
  9.         static let defaultCellRowHeight = 44
  10.         static let seperatorLineHeight: CGFloat = 0.5
  11.     }
  12.    
  13.     // MARK: - Public Properties
  14.    
  15.     weak var delegate: PopoverViewDelegate?
  16.    
  17.     // MARK: - Private Properties
  18.    
  19.     private var actionCells: [UITableViewCell]!
  20.  
  21.     // MARK: - Lifecycle
  22.    
  23.     static func create(on view: UIView, withItems items: [String], atPosition position: CGPoint, with popoverPresentationControllerDelegate: UIPopoverPresentationControllerDelegate, anchorView: UIView) -> PopoverView {
  24.         let popOverView = UIStoryboard(name: "Popover", bundle: nil).instantiateInitialViewController() as! PopoverView
  25.         popOverView.actionCells = createCells(withItems: items)
  26.         let popOverHeight = CGFloat(popOverView.actionCells.count * Constant.defaultCellRowHeight) - Constant.seperatorLineHeight // Subracting separator lineheight avoids showing last seperator line.
  27.         popOverView.preferredContentSize = CGSize(width: calculateMaxWidth(from: popOverView.actionCells), height: popOverHeight)
  28.         popOverView.modalPresentationStyle = UIModalPresentationStyle.popover
  29.         let popOver: UIPopoverPresentationController = popOverView.popoverPresentationController!
  30.        
  31.         // Set direction of the popover (should it be displayed below or under the trigger position)
  32.         if (position.y + popOverHeight) > view.bounds.height {
  33.             popOver.permittedArrowDirections = .down
  34.             popOver.sourceRect = CGRect(x: anchorView.bounds.origin.x, y: anchorView.bounds.origin.y + 8, width: anchorView.bounds.width, height: anchorView.bounds.height)
  35.         } else {
  36.             popOver.permittedArrowDirections = .up
  37.             popOver.sourceRect = CGRect(x: anchorView.bounds.origin.x, y: anchorView.bounds.origin.y - 8, width: anchorView.bounds.width, height: anchorView.bounds.height)
  38.         }
  39.        
  40.         popOver.passthroughViews = [view]
  41.         popOver.delegate = popoverPresentationControllerDelegate
  42.         popOver.sourceView = anchorView
  43.         popOver.backgroundColor = UIColor.white
  44.        
  45.         return popOverView
  46.     }
  47.    
  48.     // MARK: - Private Methods
  49.    
  50.     private static func createCells(withItems items: [String]) -> [UITableViewCell] {
  51.         var cells = [UITableViewCell]()
  52.        
  53.         items.forEach { item in
  54.             let cell = UITableViewCell()
  55.             cell.textLabel?.text = item
  56.             cell.textLabel?.translatesAutoresizingMaskIntoConstraints = false
  57.             cells.append(cell)
  58.         }
  59.        
  60.         return cells
  61.     }
  62.    
  63.     private static func calculateMaxWidth(from cells: [UITableViewCell]) -> CGFloat {
  64.         var maxWidth: CGFloat = 0
  65.        
  66.         cells.forEach { cell in
  67.             let calculatedWidth = cell.textLabel!.intrinsicContentSize.width + 32
  68.             if maxWidth < calculatedWidth {
  69.                 maxWidth = calculatedWidth
  70.             }
  71.         }
  72.        
  73.         return maxWidth
  74.     }
  75.    
  76.     // MARK: - Table view data source
  77.  
  78.     override func numberOfSections(in tableView: UITableView) -> Int {
  79.         return 1
  80.     }
  81.  
  82.     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  83.         return actionCells.count
  84.     }
  85.    
  86.     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  87.         return actionCells[indexPath.row]
  88.     }
  89.    
  90.     override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  91.         delegate?.popoverViewItemSelected(popoverTableViewController: self, atIndex: indexPath.row)
  92.     }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement