UIViewController+Extension.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // UIViewController+Extension.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2018/4/9.
  6. // Copyright © 2018年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import JHTAlertController
  10. import ProgressHUDSwift
  11. import Whisper
  12. import DatePickerDialogSwift
  13. extension UIViewController {
  14. func setNavRightBarItemsTextDefault() {
  15. if let rightItems = self.navigationItem.rightBarButtonItems {
  16. for item in rightItems {
  17. item.setTitleTextAttributes([NSAttributedString.Key.font : UIFont(name: "PingFangTC-Regular", size: 14)!], for: .normal)
  18. }
  19. }
  20. }
  21. func setNavLeftBarItemsTextDefault() {
  22. if let leftItems = self.navigationItem.leftBarButtonItems {
  23. for item in leftItems {
  24. item.setTitleTextAttributes([NSAttributedString.Key.font : UIFont(name: "PingFangTC-Regular", size: 14)!], for: .normal)
  25. }
  26. }
  27. }
  28. func forwardDestVC(_ storyBoardName:String,_ destVCIdentitifer:String?){
  29. let window = UIApplication.shared.keyWindow
  30. let storyBoard:UIStoryboard = UIStoryboard.init(name: storyBoardName, bundle: nil)
  31. var destVC:UIViewController!
  32. if destVCIdentitifer != nil {
  33. destVC = storyBoard.instantiateViewController(withIdentifier: destVCIdentitifer!)
  34. }else{
  35. destVC = storyBoard.instantiateInitialViewController()
  36. }
  37. window?.rootViewController = destVC
  38. window?.makeKeyAndVisible()
  39. }
  40. func forwardDestVC(_ targetVC:UIViewController){
  41. let window = UIApplication.shared.keyWindow
  42. window?.rootViewController = targetVC
  43. window?.makeKeyAndVisible()
  44. }
  45. func getDestVC<T>(vcType:T.Type,storyBoardName:String,identitiferController:String?) -> T {
  46. let storyBoard = UIStoryboard(name: storyBoardName, bundle: nil)
  47. var destVC:T
  48. if let identitifer = identitiferController {
  49. destVC = storyBoard.instantiateViewController(withIdentifier: identitifer) as! T
  50. }else{
  51. destVC = storyBoard.instantiateInitialViewController() as! T
  52. }
  53. return destVC
  54. }
  55. }
  56. // MARK:- AlertController
  57. extension UIViewController {
  58. /// 系统弹出框确认
  59. ///
  60. /// - Parameters:
  61. /// - title: 标题
  62. /// - message: 提示消息
  63. /// - okHandler: 确定行为
  64. func showDefaultConfirm(title: String, message: String, okHandler: @escaping ((UIAlertAction) -> Void)) {
  65. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  66. let okAction = UIAlertAction(title: "确定", style: .default, handler: okHandler)
  67. let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
  68. alertController.addAction(okAction)
  69. alertController.addAction(cancelAction)
  70. self.present(alertController, animated: true, completion: nil)
  71. }
  72. /// 系统弹出框提示
  73. ///
  74. /// - Parameters:
  75. /// - title: 标题
  76. /// - message: 提示消息
  77. /// - okHandler: 确定行为
  78. func showSystemAlert(title: String, message: String, okHandler: @escaping ((UIAlertAction) -> Void)) {
  79. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  80. let okAction = UIAlertAction(title: "确定", style: .default, handler: okHandler)
  81. alertController.addAction(okAction)
  82. self.present(alertController, animated: true, completion: nil)
  83. }
  84. // actionSheet 形式的弹出提示框 可以传入多个Action 已经有取消Action了
  85. func showSheetAction(title: String?, message: String?, actions: [UIAlertAction]) {
  86. let alertController = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
  87. actions.forEach { (action) in
  88. alertController.addAction(action)
  89. }
  90. let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
  91. alertController.addAction(cancelAction)
  92. self.present(alertController, animated: true, completion: nil)
  93. }
  94. func showAlert(title:String,message:String,okHandler:((JHTAlertAction) -> Void)!,cancelHandler:((JHTAlertAction) -> Void)!){
  95. let alertController = JHTAlertController(title: "", message: message, preferredStyle: .alert)
  96. //alertController.view.backgroundColor = UIColor(hexString: "#FCFCFC", alpha: 0.9)!
  97. //alertController.alertMessageViewBackgroundColor = UIColor(hexString: "#FCFCFC", alpha: 0.9)!
  98. alertController.titleImage = #imageLiteral(resourceName: "logo80-bai")
  99. alertController.messageTextColor = UIColor(hex: "#030303")
  100. alertController.titleViewBackgroundColor = UIColor.hexInt(0xFB4747)
  101. alertController.alertBackgroundColor = UIColor(hexString: "#FCFCFC", alpha: 0.9)!
  102. alertController.setAllButtonBackgroundColors(to: UIColor(hexString: "#FCFCFC", alpha: 0.9)!)
  103. alertController.setButtonTextColorFor(.default, to: UIColor(hex: "#FB4747"))
  104. alertController.setButtonTextColorFor(.cancel, to: UIColor(hex: "#FB4747"))
  105. alertController.hasRoundedCorners = true
  106. let okAction = JHTAlertAction(title: "确定", style: .default, handler: okHandler)
  107. let cancelAction = JHTAlertAction(title: "取消", style: .cancel, handler: cancelHandler)
  108. alertController.addActions([cancelAction,okAction])
  109. present(alertController, animated: true, completion: nil)
  110. }
  111. func datePickerTapped(_ title:String,_ dateType:UIDatePicker.Mode,_ format:String,callBackResult:((_ result:Date) -> Void)?) {
  112. let locale = Locale(identifier: "zh")
  113. let theDate = Date()
  114. var dateComponents = DateComponents()
  115. dateComponents.month = -12
  116. let threeMonthAgo = Calendar.current.date(byAdding: dateComponents, to: theDate)
  117. dateComponents.month = 12
  118. let nextYearMonthAgo = Calendar.current.date(byAdding: dateComponents, to: theDate)
  119. let datePicker = LWDatePickerDialog(textColor: .red,
  120. buttonColor: .red,
  121. font: UIFont.boldSystemFont(ofSize: 17),
  122. locale:locale ,
  123. showCancelButton: true)
  124. datePicker.show(title,
  125. doneButtonTitle: "确定",
  126. cancelButtonTitle: "取消",
  127. defaultDate: theDate,
  128. minimumDate: threeMonthAgo,
  129. maximumDate: nextYearMonthAgo,
  130. datePickerMode: dateType) { (date) in
  131. if let dt = date {
  132. let formatter = DateFormatter()
  133. formatter.dateFormat = format
  134. let _ = formatter.string(from: dt)
  135. if let block = callBackResult {
  136. block(dt)
  137. }
  138. }
  139. }
  140. }
  141. }
  142. // MARK:- ProgressHUD
  143. extension UIViewController {
  144. func showSuccess(title:String) {
  145. DispatchQueue.main.async {
  146. ProgressSHD.showSuccess(title)
  147. }
  148. }
  149. func showError(title:String){
  150. DispatchQueue.main.async {
  151. ProgressSHD.showError(title)
  152. }
  153. }
  154. func showMessage(title:String){
  155. DispatchQueue.main.async {
  156. ProgressSHD.show(title)
  157. }
  158. }
  159. func dismissProgressHUD() {
  160. DispatchQueue.main.async {
  161. ProgressSHD.dismiss()
  162. }
  163. }
  164. }
  165. // MARK:- 加动画退出app
  166. extension UIViewController {
  167. func exitAPP() {
  168. let appDelegate = UIApplication.shared.delegate!
  169. let window = appDelegate.window!
  170. UIView.animate(withDuration: 0.4, animations: {
  171. UIView.animate(withDuration: 0.4) {
  172. window?.alpha = 0
  173. let y = window?.bounds.size.height
  174. let x = (window?.bounds.size.width)! / 2
  175. window?.frame = CGRect(x: x, y: y!, width: 0, height: 0)
  176. }
  177. }) { (completed) in
  178. exit(0)
  179. }
  180. }
  181. }
  182. // MARK:- Whisper
  183. extension UIViewController {
  184. func showWhisperMessage(title:String,message:String) {
  185. if let nav = self.navigationController {
  186. let msg = Message(title: title, backgroundColor: .red)
  187. // Show and hide a message after delay
  188. Whisper.show(whisper: msg, to: nav, action: .show)
  189. // Present a permanent message
  190. Whisper.show(whisper: msg, to: nav, action: .present)
  191. // Hide a message
  192. Whisper.hide(whisperFrom: nav, after: 3)
  193. }else{
  194. let murmur = Murmur(title:title)
  195. // Present a permanent status bar message
  196. Whisper.show(whistle: murmur, action: .show(3))
  197. }
  198. }
  199. }
  200. //MARK: - 跳转到设置页面
  201. extension UIViewController {
  202. ///跳转到应用设置页面
  203. ///
  204. /// - Parameter alertMessage: 确认提示消息,如果是nil就直接跳转
  205. func gotoApplicationSettings(alertMessage:String? = nil) {
  206. if alertMessage != nil {
  207. showDefaultConfirm(title: "提示", message: alertMessage!, okHandler: { (okAction) in
  208. UIApplication.shared.openURL(URL(string: UIApplication.openSettingsURLString)!)
  209. })
  210. }else {
  211. UIApplication.shared.openURL(URL(string: UIApplication.openSettingsURLString)!)
  212. }
  213. }
  214. }