BaseViewController.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // BaseViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by 方振兴 on 2018/3/23.
  6. // Copyright © 2018年 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. import NVActivityIndicatorView
  10. import JXSegmentedView
  11. /// 管理类容器基类
  12. class BaseViewController: UIViewController {
  13. // MARK: - 属性列表
  14. /// 无数据按钮
  15. @IBOutlet weak var noDataButton: UIButton! {
  16. didSet {
  17. noDataButton.set(image: #imageLiteral(resourceName: "noData-universal"), title: "无数据", titlePosition: .bottom, additionalSpacing: 3.0, state: .normal)
  18. noDataButton.tintColor = UIColor.lightGray
  19. noDataButton.isHidden = true
  20. }
  21. }
  22. /// 兼容IOS 11
  23. var insert: UIEdgeInsets {
  24. var insets = UIEdgeInsets.zero
  25. if #available(iOS 11.0, *) {
  26. insets = UIApplication.shared.delegate?.window??.safeAreaInsets ??
  27. UIEdgeInsets.zero
  28. }
  29. return insets
  30. }
  31. /// 动画对象
  32. var _anim: NVActivityIndicatorView?
  33. /// appDelegate
  34. let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
  35. /// 当前所处位置
  36. var selectedIndex: Int = 0
  37. /// 数据携带
  38. var takeInfo: Any?
  39. /// indicator
  40. let indicator: JXSegmentedIndicatorImageView = {
  41. $0.indicatorWidth = 40.0
  42. $0.indicatorHeight = 10.0
  43. $0.image = UIImage(named: "segment_line")
  44. return $0
  45. } (JXSegmentedIndicatorImageView())
  46. // MARK: - 生命周期
  47. required init?(coder aDecoder: NSCoder) {
  48. super.init(coder: aDecoder)
  49. /// 设置Tabbar Item
  50. self.tabBarItem.setTitleTextAttributes( [NSAttributedString.Key.foregroundColor: Color_BarItem_Selected], for: .selected)
  51. self.tabBarItem.setTitleTextAttributes( [NSAttributedString.Key.foregroundColor: Color_BarItem_Normal], for: .normal)
  52. }
  53. override func viewDidLoad() {
  54. super.viewDidLoad()
  55. // Do any additional setup after loading the view.
  56. addBackBarButtonItem(false)
  57. UINavigationBar.appearance().barTintColor = UIColorFromHex(rgbValue: 0x2481DD)
  58. UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
  59. }
  60. deinit {
  61. dPrint("[\(#function)] \(self.description) 被释放了")
  62. }
  63. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  64. self.view.endEditing(true)
  65. }
  66. // MARK: - Loding动画
  67. /// 加载页面
  68. func addLoadingView() {
  69. /// Loading
  70. _anim = NVActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50), type: .ballClipRotate, color: UIColorFromHex(rgbValue: 0xA8B6CC), padding: 0)
  71. _anim!.center = self.view.center
  72. self.view.addSubview(_anim!)
  73. }
  74. /// 添加返回按钮
  75. /// - Parameter isHidden: 是否显示
  76. func addBackBarButtonItem(_ isHidden: Bool = false) {
  77. if !isHidden {
  78. self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "back"), style: .done, target: self, action: #selector(onBackBarButtonItemPressed))
  79. self.navigationItem.leftBarButtonItem?.tintColor = .white
  80. } else {
  81. self.navigationItem.leftBarButtonItem = nil
  82. }
  83. }
  84. // MARK: - 页面跳转
  85. /// present
  86. /// - Parameters:
  87. /// - sbname: sbname
  88. /// - sbId: sbId
  89. /// - isNav: isNav
  90. /// - style: modalTransitionStyle
  91. /// - takeInfo: takeInfo
  92. /// - checkLogin: checkLogin
  93. func present(storyboardName sbname:String,
  94. storyboardId sbId:String,
  95. needNavigationController isNav: Bool = true,
  96. modalTransitionStyle style: UIModalTransitionStyle = .crossDissolve,
  97. takeInfo: Any? = nil,
  98. checkLogin: Bool = true) {
  99. if !checkLogin || MTP2BusinessCore.shared.getLoginStatus() { /// 检查登录状态
  100. let board: UIStoryboard = UIStoryboard.init(name: sbname, bundle: nil)
  101. let viewController = board.instantiateViewController(withIdentifier: sbId) as? BaseViewController
  102. viewController?.takeInfo = takeInfo
  103. if isNav == true {
  104. let navigationController = BaseNavigationController(rootViewController: viewController!)
  105. navigationController.modalPresentationStyle = .overFullScreen
  106. navigationController.modalTransitionStyle = style
  107. self.present(navigationController, animated: true, completion: nil)
  108. } else {
  109. viewController?.modalPresentationStyle = .overFullScreen
  110. viewController?.modalTransitionStyle = style
  111. self.present(viewController!, animated: true, completion: nil)
  112. }
  113. } else { /// 未登录
  114. let login = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Login")
  115. let navigationController = BaseNavigationController(rootViewController: login)
  116. navigationController.modalPresentationStyle = .overFullScreen
  117. navigationController.modalTransitionStyle = style
  118. self.present(navigationController, animated: true, completion: nil)
  119. }
  120. }
  121. /// 页面消失
  122. @IBAction func dismiss() {
  123. /// 返回上层视图
  124. self.dismiss(animated: true, completion: {})
  125. }
  126. /// 返回按钮点击事件
  127. @IBAction func onBackBarButtonItemPressed() {
  128. if (self.navigationController?.viewControllers.count ?? 0) > 1 {
  129. self.navigationController?.popViewController(animated: true)
  130. } else {
  131. self.dismiss(animated: true, completion: {})
  132. }
  133. }
  134. // MARK: - 系统权限相关
  135. /// 系统权限设置
  136. func openSystemSetting() {
  137. /// 进入系统权限设置
  138. /// 这里去引导用户去获取相关权限
  139. if #available(iOS 10.0, *) {
  140. UIApplication.shared.open(URL.init(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: { (isCanOpen) in
  141. })
  142. } else {
  143. // Fallback on earlier versions
  144. UIApplication.shared.openURL(URL.init(string: UIApplication.openSettingsURLString)!)
  145. }
  146. }
  147. }
  148. extension BaseViewController: UIGestureRecognizerDelegate {
  149. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
  150. return true
  151. }
  152. func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
  153. return !(self.children.count == 1)
  154. }
  155. }