UIViewController+Extension.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 Chrysan
  10. import Photos
  11. extension UIViewController {
  12. ///EZSE: Pushes a view controller onto the receiver’s stack and updates the display.
  13. open func pushVC(_ vc: UIViewController) {
  14. navigationController?.pushViewController(vc, animated: true)
  15. }
  16. ///EZSE: Pops the top view controller from the navigation stack and updates the display.
  17. open func popVC() {
  18. _ = navigationController?.popViewController(animated: true)
  19. }
  20. /// EZSE: Added extension for popToRootViewController
  21. open func popToRootVC() {
  22. _ = navigationController?.popToRootViewController(animated: true)
  23. }
  24. ///EZSE: Presents a view controller modally.
  25. open func presentVC(_ vc: UIViewController) {
  26. present(vc, animated: true, completion: nil)
  27. }
  28. ///EZSE: Dismisses the view controller that was presented modally by the view controller.
  29. open func dismissVC(completion: (() -> Void)? ) {
  30. dismiss(animated: true, completion: completion)
  31. }
  32. func setNavRightBarItemsTextDefault() {
  33. if let rightItems = self.navigationItem.rightBarButtonItems {
  34. for item in rightItems {
  35. item.setTitleTextAttributes([NSAttributedString.Key.font : UIFont(name: "PingFangTC-Regular", size: 14)!], for: .normal)
  36. }
  37. }
  38. }
  39. func setNavLeftBarItemsTextDefault() {
  40. if let leftItems = self.navigationItem.leftBarButtonItems {
  41. for item in leftItems {
  42. item.setTitleTextAttributes([NSAttributedString.Key.font : UIFont(name: "PingFangTC-Regular", size: 14)!], for: .normal)
  43. }
  44. }
  45. }
  46. func forwardDestVC(_ storyBoardName:String,_ destVCIdentitifer:String?){
  47. let window = UIApplication.shared.keyWindow
  48. let storyBoard:UIStoryboard = UIStoryboard.init(name: storyBoardName, bundle: nil)
  49. var destVC:UIViewController!
  50. if destVCIdentitifer != nil {
  51. destVC = storyBoard.instantiateViewController(withIdentifier: destVCIdentitifer!)
  52. }else{
  53. destVC = storyBoard.instantiateInitialViewController()
  54. }
  55. window?.rootViewController = destVC
  56. window?.makeKeyAndVisible()
  57. }
  58. func forwardDestVC(_ targetVC:UIViewController){
  59. let window = UIApplication.shared.keyWindow
  60. window?.rootViewController = targetVC
  61. window?.makeKeyAndVisible()
  62. }
  63. func getDestVC<T>(vcType:T.Type,storyBoardName:String,identitiferController:String?) -> T {
  64. let storyBoard = UIStoryboard(name: storyBoardName, bundle: nil)
  65. var destVC:T
  66. if let identitifer = identitiferController {
  67. destVC = storyBoard.instantiateViewController(withIdentifier: identitifer) as! T
  68. }else{
  69. destVC = storyBoard.instantiateInitialViewController() as! T
  70. }
  71. return destVC
  72. }
  73. }
  74. // MARK:- AlertController
  75. extension UIViewController {
  76. /// 系统弹出框确认
  77. ///
  78. /// - Parameters:
  79. /// - title: 标题
  80. /// - message: 提示消息
  81. /// - okHandler: 确定行为
  82. func showDefaultConfirm(title: String, message: String, okHandler: @escaping ((UIAlertAction) -> Void)) {
  83. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  84. let okAction = UIAlertAction(title: "确定", style: .default, handler: okHandler)
  85. let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
  86. alertController.addAction(okAction)
  87. alertController.addAction(cancelAction)
  88. self.present(alertController, animated: true, completion: nil)
  89. }
  90. func showDefaultConfirm(title: String, message: String, okAction: UIAlertAction, cancelAction: UIAlertAction) {
  91. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  92. alertController.addAction(okAction)
  93. alertController.addAction(cancelAction)
  94. self.present(alertController, animated: true, completion: nil)
  95. }
  96. /// 系统弹出框提示
  97. ///
  98. /// - Parameters:
  99. /// - title: 标题
  100. /// - message: 提示消息
  101. /// - okHandler: 确定行为
  102. func showSystemAlert(title: String, message: String, okHandler: @escaping ((UIAlertAction) -> Void)) {
  103. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  104. let okAction = UIAlertAction(title: "确定", style: .default, handler: okHandler)
  105. alertController.addAction(okAction)
  106. self.present(alertController, animated: true, completion: nil)
  107. }
  108. func showSystemAlertWithButtonName(title: String, message: String, buttonName: String, okHandler: @escaping ((UIAlertAction) -> Void)) {
  109. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  110. let okAction = UIAlertAction(title: buttonName, style: .default, handler: okHandler)
  111. alertController.addAction(okAction)
  112. self.present(alertController, animated: true, completion: nil)
  113. }
  114. ///
  115. ///系统弹出窗 可以输入文字
  116. ///
  117. func showPromptAlert(title: String, message: String, inputText: String, okHandler: @escaping ((UIAlertAction, String) -> Void)) {
  118. let promptController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  119. promptController.addTextField { (textField) in
  120. textField.placeholder = "请输入..."
  121. textField.text = inputText
  122. }
  123. let okAction = UIAlertAction(title: "确定", style: .default) { (ok) in
  124. let value = promptController.textFields?.first?.text ?? ""
  125. okHandler(ok, value)
  126. }
  127. let cancelAction = UIAlertAction(title:"取消", style: .cancel, handler: nil)
  128. promptController.addAction(okAction)
  129. promptController.addAction(cancelAction)
  130. self.present(promptController, animated: true, completion: nil)
  131. }
  132. // actionSheet 形式的弹出提示框 可以传入多个Action 已经有取消Action了
  133. func showSheetAction(title: String?, message: String?, actions: [UIAlertAction]) {
  134. let alertController = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
  135. actions.forEach { (action) in
  136. alertController.addAction(action)
  137. }
  138. let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
  139. alertController.addAction(cancelAction)
  140. self.present(alertController, animated: true, completion: nil)
  141. }
  142. //actionSheet 传入的actions需要包含取消Action
  143. func showActionSheetIncludeCancelBtn(title: String?, message: String?, actions: [UIAlertAction]) {
  144. let alertController = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
  145. actions.forEach { (action) in
  146. alertController.addAction(action)
  147. }
  148. self.present(alertController, animated: true, completion: nil)
  149. }
  150. //通讯录选择器
  151. func showContactPicker(modes: [ContactPickerType],
  152. callback: @escaping (O2BizContactPickerResult) -> Void,
  153. topUnitList: [String] = [],
  154. maxNumber: Int = 0,
  155. multiple: Bool = true,
  156. initDeptPickedArray:[String] = [],
  157. initIdPickedArray:[String] = [],
  158. initGroupPickedArray:[String] = [],
  159. initUserPickedArray:[String] = []) {
  160. if let v = ContactPickerViewController.providePickerVC(
  161. pickerModes:modes,
  162. topUnitList: topUnitList,
  163. unitType: "",
  164. maxNumber: maxNumber,
  165. multiple: multiple,
  166. dutyList: [],
  167. initDeptPickedArray: initDeptPickedArray,
  168. initIdPickedArray: initIdPickedArray,
  169. initGroupPickedArray: initGroupPickedArray,
  170. initUserPickedArray: initUserPickedArray,
  171. pickedDelegate: callback
  172. ) {
  173. self.navigationController?.pushViewController(v, animated: true)
  174. }
  175. }
  176. func datePickerTapped(_ title:String,_ dateType:UIDatePicker.Mode,_ format:String,callBackResult:((_ result:Date) -> Void)?) {
  177. let locale = Locale.current
  178. let theDate = Date()
  179. var dateComponents = DateComponents()
  180. dateComponents.month = -12
  181. let threeMonthAgo = Calendar.current.date(byAdding: dateComponents, to: theDate)
  182. dateComponents.month = 12
  183. let nextYearMonthAgo = Calendar.current.date(byAdding: dateComponents, to: theDate)
  184. let datePicker = LWDatePickerDialog(textColor: .red,
  185. buttonColor: .red,
  186. font: UIFont.boldSystemFont(ofSize: 17),
  187. locale:locale ,
  188. showCancelButton: true)
  189. datePicker.show(title,
  190. doneButtonTitle: "确定",
  191. cancelButtonTitle: "取消",
  192. defaultDate: theDate,
  193. minimumDate: threeMonthAgo,
  194. maximumDate: nextYearMonthAgo,
  195. datePickerMode: dateType) { (date) in
  196. if let dt = date {
  197. let formatter = DateFormatter()
  198. formatter.dateFormat = format
  199. let _ = formatter.string(from: dt)
  200. if let block = callBackResult {
  201. block(dt)
  202. }
  203. }
  204. }
  205. }
  206. }
  207. // MARK:- ProgressHUD
  208. extension UIViewController {
  209. func showMessage(msg: String) {
  210. DispatchQueue.main.async {
  211. if self.navigationController != nil {
  212. self.navigationController?.chrysan.show(.plain, message: msg, hideDelay: 1)
  213. }else {
  214. self.chrysan.show(.plain, message: msg, hideDelay: 1)
  215. }
  216. }
  217. }
  218. func showSuccess(title:String) {
  219. DispatchQueue.main.async {
  220. if self.navigationController != nil {
  221. self.navigationController?.chrysan.show(.succeed, message: title, hideDelay: 1)
  222. }else {
  223. self.chrysan.show(.succeed, message: title, hideDelay: 1)
  224. }
  225. }
  226. }
  227. func showError(title:String){
  228. DispatchQueue.main.async {
  229. if self.navigationController != nil {
  230. self.navigationController?.chrysan.show(.error, message: title, hideDelay: 1)
  231. }else {
  232. self.chrysan.show(.error, message: title, hideDelay: 1)
  233. }
  234. }
  235. }
  236. func showLoading(title:String){
  237. DispatchQueue.main.async {
  238. if self.navigationController != nil {
  239. self.navigationController?.chrysan.show(.running, message: title)
  240. }else {
  241. self.chrysan.show(.running, message: title)
  242. }
  243. }
  244. }
  245. func showLoading() {
  246. DispatchQueue.main.async {
  247. if self.navigationController != nil {
  248. self.navigationController?.chrysan.show()
  249. }else {
  250. self.chrysan.show()
  251. }
  252. }
  253. }
  254. func hideLoading() {
  255. DispatchQueue.main.async {
  256. if self.navigationController != nil {
  257. self.navigationController?.chrysan.hide()
  258. }else {
  259. self.chrysan.hide()
  260. }
  261. }
  262. }
  263. }
  264. // MARK:- 加动画退出app
  265. extension UIViewController {
  266. func exitAPP() {
  267. let appDelegate = UIApplication.shared.delegate!
  268. let window = appDelegate.window!
  269. UIView.animate(withDuration: 0.4, animations: {
  270. UIView.animate(withDuration: 0.4) {
  271. window?.alpha = 0
  272. let y = window?.bounds.size.height
  273. let x = (window?.bounds.size.width)! / 2
  274. window?.frame = CGRect(x: x, y: y!, width: 0, height: 0)
  275. }
  276. }) { (completed) in
  277. exit(0)
  278. }
  279. }
  280. }
  281. // MARK: - 业务工具
  282. extension UIViewController {
  283. func takePhoto(delegate: (UIImagePickerControllerDelegate & UINavigationControllerDelegate)?) {
  284. var sourceType = UIImagePickerController.SourceType.camera
  285. if !UIImagePickerController.isSourceTypeAvailable(sourceType) {
  286. sourceType = .photoLibrary
  287. }
  288. let picker = UIImagePickerController()
  289. picker.allowsEditing = true
  290. picker.sourceType = sourceType
  291. picker.delegate = delegate
  292. self.present(picker, animated:true, completion:nil)//进入照相界面
  293. }
  294. //照片选择器
  295. func choosePhotoWithImagePicker(callback: @escaping (String, Data)-> Void) {
  296. let vc = FileBSImagePickerViewController().bsImagePicker()
  297. vc.settings.fetch.assets.supportedMediaTypes = [.image]
  298. presentImagePicker(vc, select: nil, deselect: nil, cancel: nil, finish: {
  299. (arr) in
  300. let count = arr.count
  301. print("选择了照片数量:\(count)")
  302. if count > 0 {
  303. //获取照片
  304. let asset = arr[0]
  305. switch asset.mediaType {
  306. case .image:
  307. let options = PHImageRequestOptions()
  308. options.isSynchronous = true
  309. options.deliveryMode = .fastFormat
  310. options.resizeMode = .none
  311. PHImageManager.default().requestImageData(for: asset, options: options) { (imageData, result, imageOrientation, dict) in
  312. guard let data = imageData else {
  313. return
  314. }
  315. var newData = data
  316. //处理图片旋转的问题
  317. if imageOrientation != UIImage.Orientation.up {
  318. let newImage = UIImage(data: data)?.fixOrientation()
  319. if newImage != nil {
  320. newData = newImage!.pngData()!
  321. }
  322. }
  323. var fileName = ""
  324. if dict?["PHImageFileURLKey"] != nil {
  325. let fileURL = dict?["PHImageFileURLKey"] as! URL
  326. fileName = fileURL.lastPathComponent
  327. } else {
  328. fileName = "\(UUID().uuidString).png"
  329. }
  330. callback(fileName, newData)
  331. }
  332. break
  333. case .video:
  334. print("视频文件。还不支持。。。。。")
  335. break
  336. default :
  337. print("未知类型的文件。。。。。。")
  338. break
  339. }
  340. }
  341. }, completion: nil)
  342. }
  343. }
  344. //MARK: - Notification
  345. extension UIViewController {
  346. ///EZSE: Adds an NotificationCenter with name and Selector
  347. open func addNotificationObserver(_ name: String, selector: Selector) {
  348. NotificationCenter.default.addObserver(self, selector: selector, name: NSNotification.Name(rawValue: name), object: nil)
  349. }
  350. ///EZSE: Removes an NSNotificationCenter for name
  351. open func removeNotificationObserver(_ name: String) {
  352. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: name), object: nil)
  353. }
  354. ///EZSE: Removes NotificationCenter'd observer
  355. open func removeNotificationObserver() {
  356. NotificationCenter.default.removeObserver(self)
  357. }
  358. ///EZSE: Adds a NotificationCenter Observer for keyboardWillShowNotification()
  359. ///
  360. /// ⚠️ You also need to implement ```keyboardWillShowNotification(_ notification: Notification)```
  361. open func addKeyboardWillShowNotification() {
  362. self.addNotificationObserver(UIResponder.keyboardWillShowNotification.rawValue, selector: #selector(UIViewController.keyboardWillShowNotification(_:)))
  363. }
  364. ///EZSE: Adds a NotificationCenter Observer for keyboardDidShowNotification()
  365. ///
  366. /// ⚠️ You also need to implement ```keyboardDidShowNotification(_ notification: Notification)```
  367. public func addKeyboardDidShowNotification() {
  368. self.addNotificationObserver(UIResponder.keyboardDidShowNotification.rawValue, selector: #selector(UIViewController.keyboardDidShowNotification(_:)))
  369. }
  370. ///EZSE: Adds a NotificationCenter Observer for keyboardWillHideNotification()
  371. ///
  372. /// ⚠️ You also need to implement ```keyboardWillHideNotification(_ notification: Notification)```
  373. open func addKeyboardWillHideNotification() {
  374. self.addNotificationObserver(UIResponder.keyboardWillHideNotification.rawValue, selector: #selector(UIViewController.keyboardWillHideNotification(_:)))
  375. }
  376. ///EZSE: Adds a NotificationCenter Observer for keyboardDidHideNotification()
  377. ///
  378. /// ⚠️ You also need to implement ```keyboardDidHideNotification(_ notification: Notification)```
  379. open func addKeyboardDidHideNotification() {
  380. self.addNotificationObserver(UIResponder.keyboardDidHideNotification.rawValue, selector: #selector(UIViewController.keyboardDidHideNotification(_:)))
  381. }
  382. ///EZSE: Removes keyboardWillShowNotification()'s NotificationCenter Observer
  383. open func removeKeyboardWillShowNotification() {
  384. self.removeNotificationObserver(UIResponder.keyboardWillShowNotification.rawValue)
  385. }
  386. ///EZSE: Removes keyboardDidShowNotification()'s NotificationCenter Observer
  387. open func removeKeyboardDidShowNotification() {
  388. self.removeNotificationObserver(UIResponder.keyboardDidShowNotification.rawValue)
  389. }
  390. ///EZSE: Removes keyboardWillHideNotification()'s NotificationCenter Observer
  391. open func removeKeyboardWillHideNotification() {
  392. self.removeNotificationObserver(UIResponder.keyboardWillHideNotification.rawValue)
  393. }
  394. ///EZSE: Removes keyboardDidHideNotification()'s NotificationCenter Observer
  395. open func removeKeyboardDidHideNotification() {
  396. self.removeNotificationObserver(UIResponder.keyboardDidHideNotification.rawValue)
  397. }
  398. @objc open func keyboardDidShowNotification(_ notification: Notification) {
  399. if let nInfo = (notification as NSNotification).userInfo, let value = nInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
  400. let frame = value.cgRectValue
  401. keyboardDidShowWithFrame(frame)
  402. }
  403. }
  404. @objc open func keyboardWillShowNotification(_ notification: Notification) {
  405. if let nInfo = (notification as NSNotification).userInfo, let value = nInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
  406. let frame = value.cgRectValue
  407. keyboardWillShowWithFrame(frame)
  408. }
  409. }
  410. @objc open func keyboardWillHideNotification(_ notification: Notification) {
  411. if let nInfo = (notification as NSNotification).userInfo, let value = nInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
  412. let frame = value.cgRectValue
  413. keyboardWillHideWithFrame(frame)
  414. }
  415. }
  416. @objc open func keyboardDidHideNotification(_ notification: Notification) {
  417. if let nInfo = (notification as NSNotification).userInfo, let value = nInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
  418. let frame = value.cgRectValue
  419. keyboardDidHideWithFrame(frame)
  420. }
  421. }
  422. @objc open func keyboardWillShowWithFrame(_ frame: CGRect) {
  423. }
  424. @objc open func keyboardDidShowWithFrame(_ frame: CGRect) {
  425. }
  426. @objc open func keyboardWillHideWithFrame(_ frame: CGRect) {
  427. }
  428. @objc open func keyboardDidHideWithFrame(_ frame: CGRect) {
  429. }
  430. //EZSE: Makes the UIViewController register tap events and hides keyboard when clicked somewhere in the ViewController.
  431. open func hideKeyboardWhenTappedAround(cancelTouches: Bool = false) {
  432. let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
  433. tap.cancelsTouchesInView = cancelTouches
  434. view.addGestureRecognizer(tap)
  435. }
  436. //EZSE: Dismisses keyboard
  437. @objc open func dismissKeyboard() {
  438. view.endEditing(true)
  439. }
  440. ///跳转到应用设置页面
  441. ///
  442. /// - Parameter alertMessage: 确认提示消息,如果是nil就直接跳转
  443. func gotoApplicationSettings(alertMessage:String? = nil) {
  444. if alertMessage != nil {
  445. showDefaultConfirm(title: "提示", message: alertMessage!, okHandler: { (okAction) in
  446. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
  447. })
  448. }else {
  449. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
  450. }
  451. }
  452. }