BaseViewController.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // BaseViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Muchinfo on 2020/12/28.
  6. // Copyright © 2020 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. import JXSegmentedView
  10. import IBAnimatable
  11. import NVActivityIndicatorView
  12. /// 视图容器控制类基类
  13. class BaseViewController: UIViewController {
  14. // MARK: - 属性列表
  15. /// 无数据按钮
  16. @IBOutlet weak var noDataButton: UIButton! {
  17. didSet {
  18. noDataButton.set(image: UIImage.getImage(name: "noData-universal"), title: "没有查询记录", titlePosition: .bottom, additionalSpacing: 20.0, state: .normal)
  19. noDataButton.tintColor = .hex999()
  20. noDataButton.setTitleColor(.fromHex(rgbValue: 0x68AADD), for: .normal)
  21. noDataButton.isHidden = true
  22. }
  23. }
  24. /// indicator
  25. let indicator: JXSegmentedIndicatorImageView = {
  26. $0.indicatorWidth = 110.0
  27. $0.indicatorHeight = 3.0
  28. $0.image = UIImage(named: "segment_line")
  29. return $0
  30. } (JXSegmentedIndicatorImageView())
  31. /// segmentedDataSource
  32. let dataSource: JXSegmentedTitleDataSource = {
  33. $0.titleNormalColor = .hex333()
  34. $0.titleNormalFont = .font_13
  35. $0.titleSelectedColor = .hex368FDF()
  36. $0.titleSelectedFont = .font_16
  37. $0.isTitleColorGradientEnabled = true
  38. return $0
  39. } (JXSegmentedTitleDataSource())
  40. /// backgroundIndicator
  41. let backgroundIndicator: JXSegmentedIndicatorBackgroundView = {
  42. $0.indicatorColor = .hex368FDF()
  43. return $0
  44. } (JXSegmentedIndicatorBackgroundView())
  45. /// segmentedDataSource
  46. let subDataSource: JXSegmentedTitleDataSource = {
  47. $0.titleNormalColor = .hex333()
  48. $0.titleNormalFont = .font_13
  49. $0.titleSelectedColor = .white
  50. $0.titleSelectedFont = .font_13
  51. $0.isTitleColorGradientEnabled = true
  52. return $0
  53. } (JXSegmentedTitleDataSource())
  54. /// 携带数据信息
  55. var takeInfo: Any?
  56. /// 动画对象
  57. var _anim: NVActivityIndicatorView?
  58. /// 返回回调
  59. var popBlock: ((_ takeInfo: Any?) -> Void)?
  60. /// 周期配置
  61. var options = SegmentioOptions(backgroundColor: .f6f6f8(),
  62. segmentPosition: .fixed(maxVisibleItems: 7),
  63. scrollEnabled: true,
  64. indicatorOptions: SegmentioIndicatorOptions(type: .bottom, ratio: 1.0, height: 2.0, color: .orange),
  65. horizontalSeparatorOptions: SegmentioHorizontalSeparatorOptions(type: .bottom, height: 0.5, color: UIColor.lightGray.withAlphaComponent(0.6)),
  66. verticalSeparatorOptions: SegmentioVerticalSeparatorOptions(ratio: 0.1, color: .clear),
  67. imageContentMode: .center,
  68. labelTextAlignment: .center,
  69. labelTextNumberOfLines: 1,
  70. segmentStates: SegmentioStates(
  71. defaultState: SegmentioState(
  72. backgroundColor: .clear,
  73. titleFont: .font_12,
  74. titleTextColor: .hex333()
  75. ),
  76. selectedState: SegmentioState(
  77. backgroundColor: .clear,
  78. titleFont: .font_13,
  79. titleTextColor: .orange
  80. ),
  81. highlightedState: SegmentioState(
  82. backgroundColor: UIColor.lightGray.withAlphaComponent(0.6),
  83. titleFont: .font_12,
  84. titleTextColor: .orange
  85. )
  86. ), animationDuration: 0.2)
  87. /// 提示信息视图
  88. lazy var info: UILabel = {
  89. $0.numberOfLines = 0
  90. $0.textColor = .hex333()
  91. $0.textAlignment = .center
  92. $0.font = .font_12
  93. return $0
  94. } (UILabel(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: self.view.width*0.9, height: 180.0))))
  95. // MARK: - 生命周期
  96. override func viewDidLoad() {
  97. super.viewDidLoad()
  98. // Do any additional setup after loading the view.
  99. /// UI界面初始化
  100. buildView()
  101. }
  102. override func viewWillAppear(_ animated: Bool) {
  103. super.viewWillAppear(animated)
  104. /// 不允许横屏
  105. let app = UIApplication.shared.delegate as? AppDelegate
  106. app?.isRotation = false
  107. }
  108. override func viewWillDisappear(_ animated: Bool) {
  109. super.viewDidDisappear(animated)
  110. /// 允许横屏
  111. let app = UIApplication.shared.delegate as? AppDelegate
  112. app?.isRotation = true
  113. }
  114. // MARK: - 初始化
  115. /// UI界面初始化
  116. func buildView() {
  117. // Do any additional setup after loading the view.
  118. addBackBarButtonItem(false)
  119. }
  120. /// 添加返回按钮
  121. /// - Parameter isHidden: 是否显示
  122. func addBackBarButtonItem(_ isHidden: Bool = false, _ image: String = "back") {
  123. if !isHidden {
  124. self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: image)?.withRenderingMode(.alwaysOriginal), style: .done, target: self, action: #selector(onBackBarButtonItemPressed))
  125. // self.navigationItem.leftBarButtonItem?.tintColor = .white
  126. } else {
  127. self.navigationItem.leftBarButtonItem = nil
  128. }
  129. }
  130. /// 加载页面
  131. func addLoadingView() {
  132. /// Loading
  133. _anim = NVActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50), type: .ballSpinFadeLoader, color: UIColor.loding(), padding: 0)
  134. _anim!.center = self.view.center
  135. self.view.addSubview(_anim!)
  136. }
  137. // MARK: - 页面跳转
  138. /// push
  139. /// - Parameters:
  140. /// - sbname: storyboardName
  141. /// - sbId: storyboardId
  142. /// - takeInfo: takeInfo
  143. func push(_ sbname:String, _ sbId:String, _ takeInfo: Any? = nil) {
  144. let board: UIStoryboard = UIStoryboard.init(name: sbname, bundle: nil)
  145. if let baseViewController = board.instantiateViewController(withIdentifier: sbId) as? BaseViewController,
  146. let model = takeInfo {
  147. baseViewController.takeInfo = model
  148. self.navigationController?.pushViewController(baseViewController, animated: true)
  149. } else {
  150. let viewController = board.instantiateViewController(withIdentifier: sbId)
  151. self.navigationController?.pushViewController(viewController, animated: true)
  152. }
  153. }
  154. /// present
  155. /// - Parameters:
  156. /// - sbname: sbname
  157. /// - sbId: sbId
  158. /// - isNav: isNav
  159. /// - style: modalTransitionStyle
  160. func present(_ sbname: String, _ sbId:String, _ isNav: Bool = true, _ style: UIModalTransitionStyle = .crossDissolve, _ takeInfo: Any? = nil) {
  161. let board: UIStoryboard = UIStoryboard.init(name: sbname, bundle: nil)
  162. if let viewController = board.instantiateViewController(withIdentifier: sbId) as? BaseViewController {
  163. viewController.takeInfo = takeInfo
  164. if isNav {
  165. let navigationController = BaseNavigationController(rootViewController: viewController)
  166. navigationController.modalPresentationStyle = .overFullScreen
  167. navigationController.modalTransitionStyle = style
  168. self.present(navigationController, animated: true, completion: nil)
  169. } else {
  170. viewController.modalPresentationStyle = .overFullScreen
  171. viewController.modalTransitionStyle = style
  172. self.present(viewController, animated: true, completion: nil)
  173. }
  174. } else if let viewController = board.instantiateViewController(withIdentifier: sbId) as? BaseNavigationController {
  175. viewController.modalPresentationStyle = .overFullScreen
  176. viewController.modalTransitionStyle = style
  177. self.present(viewController, animated: true, completion: nil)
  178. }
  179. }
  180. /// 返回按钮点击事件
  181. @objc func onBackBarButtonItemPressed() {
  182. if (self.navigationController?.viewControllers.count ?? 0) > 1 {
  183. self.navigationController?.popViewController(animated: true)
  184. } else {
  185. self.dismiss(animated: true, completion: {})
  186. }
  187. }
  188. }