TableViewInContainerViewController.swift 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // TableViewInContainerViewController.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 TableViewInContainerViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UIPopoverPresentationControllerDelegate {
  10. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  11. return 30
  12. }
  13. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  14. let identifier = "TestCell"
  15. var cell = tableView.dequeueReusableCell(withIdentifier: identifier)
  16. if cell == nil {
  17. cell = UITableViewCell(style: .default, reuseIdentifier: identifier)
  18. cell?.backgroundColor = UIColor.clear
  19. let contentView: UIView! = cell?.contentView
  20. let textField = UITextField(frame: CGRect(x: 10, y: 0, width: contentView.frame.size.width-20, height: 33))
  21. textField.autoresizingMask = [.flexibleBottomMargin, .flexibleTopMargin, .flexibleWidth]
  22. textField.center = contentView.center
  23. textField.backgroundColor = UIColor.clear
  24. textField.borderStyle = .roundedRect
  25. textField.tag = 123
  26. cell?.contentView.addSubview(textField)
  27. }
  28. let textField = cell?.viewWithTag(123) as? UITextField
  29. textField?.placeholder = "Cell \(indexPath.row)"
  30. return cell!
  31. }
  32. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  33. guard let identifier = segue.identifier else {
  34. return
  35. }
  36. if identifier == "SettingsNavigationController" {
  37. let controller = segue.destination
  38. controller.modalPresentationStyle = .popover
  39. controller.popoverPresentationController?.barButtonItem = sender as? UIBarButtonItem
  40. let heightWidth = max(UIScreen.main.bounds.width, UIScreen.main.bounds.height)
  41. controller.preferredContentSize = CGSize(width: heightWidth, height: heightWidth)
  42. controller.popoverPresentationController?.delegate = self
  43. }
  44. }
  45. func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
  46. return .none
  47. }
  48. func prepareForPopoverPresentation(_ popoverPresentationController: UIPopoverPresentationController) {
  49. self.view.endEditing(true)
  50. }
  51. override var shouldAutorotate: Bool {
  52. return true
  53. }
  54. }