UIViewController+Extension.swift 9.9 KB

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