UIViewController+Extension.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. }
  177. // MARK:- ProgressHUD
  178. extension UIViewController {
  179. func showMessage(msg: String) {
  180. DispatchQueue.main.async {
  181. if self.navigationController != nil {
  182. self.navigationController?.chrysan.show(.plain, message: msg, hideDelay: 1)
  183. }else {
  184. self.chrysan.show(.plain, message: msg, hideDelay: 1)
  185. }
  186. }
  187. }
  188. func showSuccess(title:String) {
  189. DispatchQueue.main.async {
  190. if self.navigationController != nil {
  191. self.navigationController?.chrysan.show(.succeed, message: title, hideDelay: 1)
  192. }else {
  193. self.chrysan.show(.succeed, message: title, hideDelay: 1)
  194. }
  195. }
  196. }
  197. func showError(title:String){
  198. DispatchQueue.main.async {
  199. if self.navigationController != nil {
  200. self.navigationController?.chrysan.show(.error, message: title, hideDelay: 1)
  201. }else {
  202. self.chrysan.show(.error, message: title, hideDelay: 1)
  203. }
  204. }
  205. }
  206. func showLoading(title:String){
  207. DispatchQueue.main.async {
  208. if self.navigationController != nil {
  209. self.navigationController?.chrysan.show(.running, message: title)
  210. }else {
  211. self.chrysan.show(.running, message: title)
  212. }
  213. }
  214. }
  215. func showLoading() {
  216. DispatchQueue.main.async {
  217. if self.navigationController != nil {
  218. self.navigationController?.chrysan.show()
  219. }else {
  220. self.chrysan.show()
  221. }
  222. }
  223. }
  224. func hideLoading() {
  225. DispatchQueue.main.async {
  226. if self.navigationController != nil {
  227. self.navigationController?.chrysan.hide()
  228. }else {
  229. self.chrysan.hide()
  230. }
  231. }
  232. }
  233. }
  234. // MARK:- 加动画退出app
  235. extension UIViewController {
  236. func exitAPP() {
  237. let appDelegate = UIApplication.shared.delegate!
  238. let window = appDelegate.window!
  239. UIView.animate(withDuration: 0.4, animations: {
  240. UIView.animate(withDuration: 0.4) {
  241. window?.alpha = 0
  242. let y = window?.bounds.size.height
  243. let x = (window?.bounds.size.width)! / 2
  244. window?.frame = CGRect(x: x, y: y!, width: 0, height: 0)
  245. }
  246. }) { (completed) in
  247. exit(0)
  248. }
  249. }
  250. }
  251. // MARK: - 业务工具
  252. extension UIViewController {
  253. func takePhoto(delegate: (UIImagePickerControllerDelegate & UINavigationControllerDelegate)?) {
  254. var sourceType = UIImagePickerController.SourceType.camera
  255. if !UIImagePickerController.isSourceTypeAvailable(sourceType) {
  256. sourceType = .photoLibrary
  257. }
  258. let picker = UIImagePickerController()
  259. picker.allowsEditing = true
  260. picker.sourceType = sourceType
  261. picker.delegate = delegate
  262. self.present(picker, animated:true, completion:nil)//进入照相界面
  263. }
  264. //照片选择器
  265. func choosePhotoWithImagePicker(callback: @escaping (String, Data)-> Void) {
  266. let vc = FileBSImagePickerViewController().bsImagePicker()
  267. // vc.settings.fetch.assets.supportedMediaTypes = [.image]
  268. presentImagePicker(vc, select: { (asset: PHAsset) -> Void in
  269. // User selected an asset.
  270. // Do something with it, start upload perhaps?
  271. }, deselect: { (asset: PHAsset) -> Void in
  272. // User deselected an assets.
  273. // Do something, cancel upload?
  274. }, cancel: { (assets: [PHAsset]) -> Void in
  275. // User cancelled. And this where the assets currently selected.
  276. }, finish: {
  277. (arr) in
  278. let count = arr.count
  279. print("选择了照片数量:\(count)")
  280. if count > 0 {
  281. //获取照片
  282. let asset = arr[0]
  283. switch asset.mediaType {
  284. case .image:
  285. let options = PHImageRequestOptions()
  286. options.isSynchronous = true
  287. options.deliveryMode = .fastFormat
  288. options.resizeMode = .none
  289. let fName = (asset.value(forKey: "filename") as? String) ?? "untitle.png"
  290. PHImageManager.default().requestImageData(for: asset, options: options) { (imageData, result, imageOrientation, dict) in
  291. guard let data = imageData else {
  292. return
  293. }
  294. var newData = data
  295. //处理图片旋转的问题
  296. if imageOrientation != UIImage.Orientation.up {
  297. let newImage = UIImage(data: data)?.fixOrientation()
  298. if newImage != nil {
  299. newData = newImage!.pngData()!
  300. }
  301. }
  302. // var fileName = ""
  303. // if dict?["PHImageFileURLKey"] != nil {
  304. // let fileURL = dict?["PHImageFileURLKey"] as! URL
  305. // fileName = fileURL.lastPathComponent
  306. // } else {
  307. // fileName = "\(UUID().uuidString).png"
  308. // }
  309. callback(fName, newData)
  310. }
  311. break
  312. case .video:
  313. print("视频文件。还不支持。。。。。")
  314. break
  315. default :
  316. print("未知类型的文件。。。。。。")
  317. break
  318. }
  319. }
  320. }, completion: nil)
  321. }
  322. }
  323. //MARK: - Notification
  324. extension UIViewController {
  325. ///EZSE: Adds an NotificationCenter with name and Selector
  326. open func addNotificationObserver(_ name: String, selector: Selector) {
  327. NotificationCenter.default.addObserver(self, selector: selector, name: NSNotification.Name(rawValue: name), object: nil)
  328. }
  329. ///EZSE: Removes an NSNotificationCenter for name
  330. open func removeNotificationObserver(_ name: String) {
  331. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: name), object: nil)
  332. }
  333. ///EZSE: Removes NotificationCenter'd observer
  334. open func removeNotificationObserver() {
  335. NotificationCenter.default.removeObserver(self)
  336. }
  337. ///EZSE: Adds a NotificationCenter Observer for keyboardWillShowNotification()
  338. ///
  339. /// ⚠️ You also need to implement ```keyboardWillShowNotification(_ notification: Notification)```
  340. open func addKeyboardWillShowNotification() {
  341. self.addNotificationObserver(UIResponder.keyboardWillShowNotification.rawValue, selector: #selector(UIViewController.keyboardWillShowNotification(_:)))
  342. }
  343. ///EZSE: Adds a NotificationCenter Observer for keyboardDidShowNotification()
  344. ///
  345. /// ⚠️ You also need to implement ```keyboardDidShowNotification(_ notification: Notification)```
  346. public func addKeyboardDidShowNotification() {
  347. self.addNotificationObserver(UIResponder.keyboardDidShowNotification.rawValue, selector: #selector(UIViewController.keyboardDidShowNotification(_:)))
  348. }
  349. ///EZSE: Adds a NotificationCenter Observer for keyboardWillHideNotification()
  350. ///
  351. /// ⚠️ You also need to implement ```keyboardWillHideNotification(_ notification: Notification)```
  352. open func addKeyboardWillHideNotification() {
  353. self.addNotificationObserver(UIResponder.keyboardWillHideNotification.rawValue, selector: #selector(UIViewController.keyboardWillHideNotification(_:)))
  354. }
  355. ///EZSE: Adds a NotificationCenter Observer for keyboardDidHideNotification()
  356. ///
  357. /// ⚠️ You also need to implement ```keyboardDidHideNotification(_ notification: Notification)```
  358. open func addKeyboardDidHideNotification() {
  359. self.addNotificationObserver(UIResponder.keyboardDidHideNotification.rawValue, selector: #selector(UIViewController.keyboardDidHideNotification(_:)))
  360. }
  361. ///EZSE: Removes keyboardWillShowNotification()'s NotificationCenter Observer
  362. open func removeKeyboardWillShowNotification() {
  363. self.removeNotificationObserver(UIResponder.keyboardWillShowNotification.rawValue)
  364. }
  365. ///EZSE: Removes keyboardDidShowNotification()'s NotificationCenter Observer
  366. open func removeKeyboardDidShowNotification() {
  367. self.removeNotificationObserver(UIResponder.keyboardDidShowNotification.rawValue)
  368. }
  369. ///EZSE: Removes keyboardWillHideNotification()'s NotificationCenter Observer
  370. open func removeKeyboardWillHideNotification() {
  371. self.removeNotificationObserver(UIResponder.keyboardWillHideNotification.rawValue)
  372. }
  373. ///EZSE: Removes keyboardDidHideNotification()'s NotificationCenter Observer
  374. open func removeKeyboardDidHideNotification() {
  375. self.removeNotificationObserver(UIResponder.keyboardDidHideNotification.rawValue)
  376. }
  377. @objc open func keyboardDidShowNotification(_ notification: Notification) {
  378. if let nInfo = (notification as NSNotification).userInfo, let value = nInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
  379. let frame = value.cgRectValue
  380. keyboardDidShowWithFrame(frame)
  381. }
  382. }
  383. @objc open func keyboardWillShowNotification(_ notification: Notification) {
  384. if let nInfo = (notification as NSNotification).userInfo, let value = nInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
  385. let frame = value.cgRectValue
  386. keyboardWillShowWithFrame(frame)
  387. }
  388. }
  389. @objc open func keyboardWillHideNotification(_ notification: Notification) {
  390. if let nInfo = (notification as NSNotification).userInfo, let value = nInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
  391. let frame = value.cgRectValue
  392. keyboardWillHideWithFrame(frame)
  393. }
  394. }
  395. @objc open func keyboardDidHideNotification(_ notification: Notification) {
  396. if let nInfo = (notification as NSNotification).userInfo, let value = nInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
  397. let frame = value.cgRectValue
  398. keyboardDidHideWithFrame(frame)
  399. }
  400. }
  401. @objc open func keyboardWillShowWithFrame(_ frame: CGRect) {
  402. }
  403. @objc open func keyboardDidShowWithFrame(_ frame: CGRect) {
  404. }
  405. @objc open func keyboardWillHideWithFrame(_ frame: CGRect) {
  406. }
  407. @objc open func keyboardDidHideWithFrame(_ frame: CGRect) {
  408. }
  409. //EZSE: Makes the UIViewController register tap events and hides keyboard when clicked somewhere in the ViewController.
  410. open func hideKeyboardWhenTappedAround(cancelTouches: Bool = false) {
  411. let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
  412. tap.cancelsTouchesInView = cancelTouches
  413. view.addGestureRecognizer(tap)
  414. }
  415. //EZSE: Dismisses keyboard
  416. @objc open func dismissKeyboard() {
  417. view.endEditing(true)
  418. }
  419. ///跳转到应用设置页面
  420. ///
  421. /// - Parameter alertMessage: 确认提示消息,如果是nil就直接跳转
  422. func gotoApplicationSettings(alertMessage:String? = nil) {
  423. if alertMessage != nil {
  424. showDefaultConfirm(title: "提示", message: alertMessage!, okHandler: { (okAction) in
  425. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
  426. })
  427. }else {
  428. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
  429. }
  430. }
  431. }