No Description

UIViewControllerExt.swift 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //
  2. // UIViewControllerExt.swift
  3. // ExtensionKit
  4. //
  5. // Created by FFIB on 2017/9/13.
  6. // Copyright © 2017年 FFIB. All rights reserved.
  7. //
  8. import UIKit
  9. /// MARK: custom methods extension
  10. public extension UIViewController {
  11. func addFullScreen(childViewController child: UIViewController) {
  12. guard child.parent == nil else { return }
  13. addChild(child)
  14. view.addSubview(child.view)
  15. child.view.frame = view.bounds
  16. if #available(iOS 12, *) {
  17. child.view.translatesAutoresizingMaskIntoConstraints = false
  18. NSLayoutConstraint.activate([
  19. child.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  20. child.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
  21. child.view.topAnchor.constraint(equalTo: view.topAnchor),
  22. child.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
  23. ].compactMap {$0})
  24. }
  25. child.didMove(toParent: self)
  26. }
  27. func remove(childViewController child: UIViewController) {
  28. guard child.parent != nil else { return }
  29. child.willMove(toParent: nil)
  30. child.view.removeFromSuperview()
  31. child.removeFromParent()
  32. }
  33. func removeFromParentAndView() {
  34. view.removeFromSuperview()
  35. removeFromParent()
  36. }
  37. }