TextSelectionViewController.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // TextSelectionViewController.swift
  3. // IQKeyboardManager
  4. //
  5. // Created by InfoEnum02 on 20/04/15.
  6. // Copyright (c) 2015 Iftekhar. All rights reserved.
  7. //
  8. import UIKit
  9. class TextSelectionViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIPopoverPresentationControllerDelegate {
  10. @IBOutlet var tableView: UITableView!
  11. private let data = ["Hello", "This is a demo code", "Issue #56", "With mutiple cells", "And some useless text.",
  12. "Hello", "This is a demo code", "Issue #56", "With mutiple cells", "And some useless text.",
  13. "Hello", "This is a demo code", "Issue #56", "With mutiple cells", "And some useless text."]
  14. func tableView(_ tableView: UITableView, heightForRowAt heightForRowAtIndexPath: IndexPath) -> CGFloat {
  15. return tableView.rowHeight
  16. }
  17. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  18. return data.count
  19. }
  20. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  21. let identifier = "\((indexPath as NSIndexPath).section) \((indexPath as NSIndexPath).row)"
  22. var cell = tableView.dequeueReusableCell(withIdentifier: identifier)
  23. if cell == nil {
  24. cell = UITableViewCell(style: .default, reuseIdentifier: identifier)
  25. cell?.selectionStyle = .none
  26. cell?.backgroundColor = UIColor.clear
  27. let textView = UITextView(frame: CGRect(x: 5, y: 7, width: 135, height: 30))
  28. textView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
  29. textView.backgroundColor = UIColor.clear
  30. textView.text = data[(indexPath as NSIndexPath).row]
  31. textView.dataDetectorTypes = UIDataDetectorTypes.all
  32. textView.isScrollEnabled = false
  33. textView.isEditable = false
  34. cell?.contentView.addSubview(textView)
  35. }
  36. return cell!
  37. }
  38. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  39. if let identifier = segue.identifier {
  40. if identifier == "SettingsNavigationController" {
  41. let controller = segue.destination
  42. controller.modalPresentationStyle = .popover
  43. controller.popoverPresentationController?.barButtonItem = sender as? UIBarButtonItem
  44. let heightWidth = max(UIScreen.main.bounds.width, UIScreen.main.bounds.height)
  45. controller.preferredContentSize = CGSize(width: heightWidth, height: heightWidth)
  46. controller.popoverPresentationController?.delegate = self
  47. }
  48. }
  49. }
  50. func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
  51. return .none
  52. }
  53. func prepareForPopoverPresentation(_ popoverPresentationController: UIPopoverPresentationController) {
  54. self.view.endEditing(true)
  55. }
  56. override var shouldAutorotate: Bool {
  57. return true
  58. }
  59. }