UIViewController+Extension.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. //照片选择器
  284. func choosePhotoWithImagePicker(callback: @escaping (String, Data)-> Void) {
  285. let chooseImage = FileBSImagePickerViewController()
  286. self.bs_presentImagePickerController(chooseImage, animated: true, select: nil, deselect: nil, cancel: nil, finish: {
  287. (arr) in
  288. let count = arr.count
  289. print("选择了照片数量:\(count)")
  290. if count > 0 {
  291. //获取照片
  292. let asset = arr[0]
  293. switch asset.mediaType {
  294. case .image:
  295. let options = PHImageRequestOptions()
  296. // options.isSynchronous = true
  297. options.deliveryMode = .fastFormat
  298. options.isNetworkAccessAllowed = true
  299. options.resizeMode = .none
  300. options.progressHandler = { progress, error, p, d in
  301. print("下载进度。。。\(progress)")
  302. print("下载错误。。。\(String(describing: error))")
  303. }
  304. PHImageManager.default().requestImage(for: asset, targetSize: PHImageManagerMaximumSize, contentMode: .aspectFill, options: options, resultHandler: { (image, dict) in
  305. if image == nil {
  306. print("选择照片出错 is nil")
  307. } else {
  308. //处理图片旋转的问题
  309. let newImage = image?.fixOrientation()
  310. let newData = newImage!.hnk_data()
  311. if newData == nil {
  312. print("照片旋转出错")
  313. }else {
  314. var fileName = "unkownFile"
  315. if let fileURL = dict?["PHImageFileURLKey"] as? URL {
  316. fileName = fileURL.lastPathComponent
  317. }
  318. callback(fileName, newData!)
  319. }
  320. }
  321. })
  322. //选择iCloud上的照片获取不到data
  323. // PHImageManager.default().requestImageData(for: asset, options: options, resultHandler: { (imageData, result, imageOrientation, dict) in
  324. // if imageData == nil {
  325. // print("选择照片出错")
  326. // }else {
  327. // var newData = imageData
  328. // //处理图片旋转的问题
  329. // if imageOrientation != UIImage.Orientation.up && imageData != nil {
  330. // let newImage = UIImage(data: imageData!)?.fixOrientation()
  331. // if newImage != nil {
  332. // newData = newImage?.pngData()
  333. // }
  334. // }
  335. // if newData == nil {
  336. // print("照片旋转出错")
  337. // }else {
  338. // var fileName = "unkownFile"
  339. // if let fileURL = dict?["PHImageFileURLKey"] as? URL {
  340. // fileName = fileURL.lastPathComponent
  341. // }
  342. // callback(fileName, newData!)
  343. // }
  344. // }
  345. // })
  346. break
  347. case .video:
  348. print("视频文件。还不支持。。。。。")
  349. break
  350. default :
  351. print("未知类型的文件。。。。。。")
  352. break
  353. }
  354. }
  355. }, completion: nil)
  356. }
  357. }
  358. //MARK: - Notification
  359. extension UIViewController {
  360. ///EZSE: Adds an NotificationCenter with name and Selector
  361. open func addNotificationObserver(_ name: String, selector: Selector) {
  362. NotificationCenter.default.addObserver(self, selector: selector, name: NSNotification.Name(rawValue: name), object: nil)
  363. }
  364. ///EZSE: Removes an NSNotificationCenter for name
  365. open func removeNotificationObserver(_ name: String) {
  366. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: name), object: nil)
  367. }
  368. ///EZSE: Removes NotificationCenter'd observer
  369. open func removeNotificationObserver() {
  370. NotificationCenter.default.removeObserver(self)
  371. }
  372. ///EZSE: Adds a NotificationCenter Observer for keyboardWillShowNotification()
  373. ///
  374. /// ⚠️ You also need to implement ```keyboardWillShowNotification(_ notification: Notification)```
  375. open func addKeyboardWillShowNotification() {
  376. self.addNotificationObserver(UIResponder.keyboardWillShowNotification.rawValue, selector: #selector(UIViewController.keyboardWillShowNotification(_:)))
  377. }
  378. ///EZSE: Adds a NotificationCenter Observer for keyboardDidShowNotification()
  379. ///
  380. /// ⚠️ You also need to implement ```keyboardDidShowNotification(_ notification: Notification)```
  381. public func addKeyboardDidShowNotification() {
  382. self.addNotificationObserver(UIResponder.keyboardDidShowNotification.rawValue, selector: #selector(UIViewController.keyboardDidShowNotification(_:)))
  383. }
  384. ///EZSE: Adds a NotificationCenter Observer for keyboardWillHideNotification()
  385. ///
  386. /// ⚠️ You also need to implement ```keyboardWillHideNotification(_ notification: Notification)```
  387. open func addKeyboardWillHideNotification() {
  388. self.addNotificationObserver(UIResponder.keyboardWillHideNotification.rawValue, selector: #selector(UIViewController.keyboardWillHideNotification(_:)))
  389. }
  390. ///EZSE: Adds a NotificationCenter Observer for keyboardDidHideNotification()
  391. ///
  392. /// ⚠️ You also need to implement ```keyboardDidHideNotification(_ notification: Notification)```
  393. open func addKeyboardDidHideNotification() {
  394. self.addNotificationObserver(UIResponder.keyboardDidHideNotification.rawValue, selector: #selector(UIViewController.keyboardDidHideNotification(_:)))
  395. }
  396. ///EZSE: Removes keyboardWillShowNotification()'s NotificationCenter Observer
  397. open func removeKeyboardWillShowNotification() {
  398. self.removeNotificationObserver(UIResponder.keyboardWillShowNotification.rawValue)
  399. }
  400. ///EZSE: Removes keyboardDidShowNotification()'s NotificationCenter Observer
  401. open func removeKeyboardDidShowNotification() {
  402. self.removeNotificationObserver(UIResponder.keyboardDidShowNotification.rawValue)
  403. }
  404. ///EZSE: Removes keyboardWillHideNotification()'s NotificationCenter Observer
  405. open func removeKeyboardWillHideNotification() {
  406. self.removeNotificationObserver(UIResponder.keyboardWillHideNotification.rawValue)
  407. }
  408. ///EZSE: Removes keyboardDidHideNotification()'s NotificationCenter Observer
  409. open func removeKeyboardDidHideNotification() {
  410. self.removeNotificationObserver(UIResponder.keyboardDidHideNotification.rawValue)
  411. }
  412. @objc open func keyboardDidShowNotification(_ notification: Notification) {
  413. if let nInfo = (notification as NSNotification).userInfo, let value = nInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
  414. let frame = value.cgRectValue
  415. keyboardDidShowWithFrame(frame)
  416. }
  417. }
  418. @objc open func keyboardWillShowNotification(_ notification: Notification) {
  419. if let nInfo = (notification as NSNotification).userInfo, let value = nInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
  420. let frame = value.cgRectValue
  421. keyboardWillShowWithFrame(frame)
  422. }
  423. }
  424. @objc open func keyboardWillHideNotification(_ notification: Notification) {
  425. if let nInfo = (notification as NSNotification).userInfo, let value = nInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
  426. let frame = value.cgRectValue
  427. keyboardWillHideWithFrame(frame)
  428. }
  429. }
  430. @objc open func keyboardDidHideNotification(_ notification: Notification) {
  431. if let nInfo = (notification as NSNotification).userInfo, let value = nInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
  432. let frame = value.cgRectValue
  433. keyboardDidHideWithFrame(frame)
  434. }
  435. }
  436. @objc open func keyboardWillShowWithFrame(_ frame: CGRect) {
  437. }
  438. @objc open func keyboardDidShowWithFrame(_ frame: CGRect) {
  439. }
  440. @objc open func keyboardWillHideWithFrame(_ frame: CGRect) {
  441. }
  442. @objc open func keyboardDidHideWithFrame(_ frame: CGRect) {
  443. }
  444. //EZSE: Makes the UIViewController register tap events and hides keyboard when clicked somewhere in the ViewController.
  445. open func hideKeyboardWhenTappedAround(cancelTouches: Bool = false) {
  446. let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
  447. tap.cancelsTouchesInView = cancelTouches
  448. view.addGestureRecognizer(tap)
  449. }
  450. //EZSE: Dismisses keyboard
  451. @objc open func dismissKeyboard() {
  452. view.endEditing(true)
  453. }
  454. ///跳转到应用设置页面
  455. ///
  456. /// - Parameter alertMessage: 确认提示消息,如果是nil就直接跳转
  457. func gotoApplicationSettings(alertMessage:String? = nil) {
  458. if alertMessage != nil {
  459. showDefaultConfirm(title: "提示", message: alertMessage!, okHandler: { (okAction) in
  460. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
  461. })
  462. }else {
  463. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
  464. }
  465. }
  466. }