UIViewController+Extension.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 ProgressHUDSwift
  10. extension UIViewController {
  11. func setNavRightBarItemsTextDefault() {
  12. if let rightItems = self.navigationItem.rightBarButtonItems {
  13. for item in rightItems {
  14. item.setTitleTextAttributes([NSAttributedString.Key.font : UIFont(name: "PingFangTC-Regular", size: 14)!], for: .normal)
  15. }
  16. }
  17. }
  18. func setNavLeftBarItemsTextDefault() {
  19. if let leftItems = self.navigationItem.leftBarButtonItems {
  20. for item in leftItems {
  21. item.setTitleTextAttributes([NSAttributedString.Key.font : UIFont(name: "PingFangTC-Regular", size: 14)!], for: .normal)
  22. }
  23. }
  24. }
  25. func forwardDestVC(_ storyBoardName:String,_ destVCIdentitifer:String?){
  26. let window = UIApplication.shared.keyWindow
  27. let storyBoard:UIStoryboard = UIStoryboard.init(name: storyBoardName, bundle: nil)
  28. var destVC:UIViewController!
  29. if destVCIdentitifer != nil {
  30. destVC = storyBoard.instantiateViewController(withIdentifier: destVCIdentitifer!)
  31. }else{
  32. destVC = storyBoard.instantiateInitialViewController()
  33. }
  34. window?.rootViewController = destVC
  35. window?.makeKeyAndVisible()
  36. }
  37. func forwardDestVC(_ targetVC:UIViewController){
  38. let window = UIApplication.shared.keyWindow
  39. window?.rootViewController = targetVC
  40. window?.makeKeyAndVisible()
  41. }
  42. func getDestVC<T>(vcType:T.Type,storyBoardName:String,identitiferController:String?) -> T {
  43. let storyBoard = UIStoryboard(name: storyBoardName, bundle: nil)
  44. var destVC:T
  45. if let identitifer = identitiferController {
  46. destVC = storyBoard.instantiateViewController(withIdentifier: identitifer) as! T
  47. }else{
  48. destVC = storyBoard.instantiateInitialViewController() as! T
  49. }
  50. return destVC
  51. }
  52. }
  53. // MARK:- AlertController
  54. extension UIViewController {
  55. /// 系统弹出框确认
  56. ///
  57. /// - Parameters:
  58. /// - title: 标题
  59. /// - message: 提示消息
  60. /// - okHandler: 确定行为
  61. func showDefaultConfirm(title: String, message: String, okHandler: @escaping ((UIAlertAction) -> Void)) {
  62. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  63. let okAction = UIAlertAction(title: "确定", style: .default, handler: okHandler)
  64. let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
  65. alertController.addAction(okAction)
  66. alertController.addAction(cancelAction)
  67. self.present(alertController, animated: true, completion: nil)
  68. }
  69. func showDefaultConfirm(title: String, message: String, okAction: UIAlertAction, cancelAction: UIAlertAction) {
  70. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  71. alertController.addAction(okAction)
  72. alertController.addAction(cancelAction)
  73. self.present(alertController, animated: true, completion: nil)
  74. }
  75. /// 系统弹出框提示
  76. ///
  77. /// - Parameters:
  78. /// - title: 标题
  79. /// - message: 提示消息
  80. /// - okHandler: 确定行为
  81. func showSystemAlert(title: String, message: String, okHandler: @escaping ((UIAlertAction) -> Void)) {
  82. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  83. let okAction = UIAlertAction(title: "确定", style: .default, handler: okHandler)
  84. alertController.addAction(okAction)
  85. self.present(alertController, animated: true, completion: nil)
  86. }
  87. func showSystemAlertWithButtonName(title: String, message: String, buttonName: String, okHandler: @escaping ((UIAlertAction) -> Void)) {
  88. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  89. let okAction = UIAlertAction(title: buttonName, style: .default, handler: okHandler)
  90. alertController.addAction(okAction)
  91. self.present(alertController, animated: true, completion: nil)
  92. }
  93. // actionSheet 形式的弹出提示框 可以传入多个Action 已经有取消Action了
  94. func showSheetAction(title: String?, message: String?, actions: [UIAlertAction]) {
  95. let alertController = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
  96. actions.forEach { (action) in
  97. alertController.addAction(action)
  98. }
  99. let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
  100. alertController.addAction(cancelAction)
  101. self.present(alertController, animated: true, completion: nil)
  102. }
  103. //actionSheet 传入的actions需要包含取消Action
  104. func showActionSheetIncludeCancelBtn(title: String?, message: String?, actions: [UIAlertAction]) {
  105. let alertController = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
  106. actions.forEach { (action) in
  107. alertController.addAction(action)
  108. }
  109. self.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: - 跳转到设置页面
  183. extension UIViewController {
  184. ///跳转到应用设置页面
  185. ///
  186. /// - Parameter alertMessage: 确认提示消息,如果是nil就直接跳转
  187. func gotoApplicationSettings(alertMessage:String? = nil) {
  188. if alertMessage != nil {
  189. showDefaultConfirm(title: "提示", message: alertMessage!, okHandler: { (okAction) in
  190. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
  191. })
  192. }else {
  193. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
  194. }
  195. }
  196. }