UIViewController+Extension.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. //选中一个
  270. }, deselect: { (asset: PHAsset) -> Void in
  271. //取消选中一个
  272. }, cancel: { (assets: [PHAsset]) -> Void in
  273. //取消
  274. }, finish: { (arr) in
  275. let count = arr.count
  276. print("选择了照片数量:\(count)")
  277. if count > 0 {
  278. //获取照片
  279. let asset = arr[0]
  280. switch asset.mediaType {
  281. case .image:
  282. let options = PHImageRequestOptions()
  283. options.isSynchronous = true
  284. options.deliveryMode = .fastFormat
  285. options.resizeMode = .none
  286. var fName = (asset.value(forKey: "filename") as? String) ?? "untitle.png"
  287. // 判断是否是heif
  288. var isHEIF = false
  289. if #available(iOS 9.0, *) {
  290. let resList = PHAssetResource.assetResources(for: asset)
  291. resList.forEachEnumerated { (idx, res) in
  292. let uti = res.uniformTypeIdentifier
  293. if uti == "public.heif" || uti == "public.heic" {
  294. isHEIF = true
  295. }
  296. }
  297. } else {
  298. if let uti = asset.value(forKey: "uniformTypeIdentifier") as? String {
  299. if uti == "public.heif" || uti == "public.heic" {
  300. isHEIF = true
  301. }
  302. }
  303. }
  304. PHImageManager.default().requestImageData(for: asset, options: options) { (imageData, result, imageOrientation, dict) in
  305. guard let data = imageData else {
  306. return
  307. }
  308. var newData = data
  309. if isHEIF {
  310. let image: UIImage = UIImage(data: data)!
  311. newData = image.jpegData(compressionQuality: 1.0)!
  312. fName += ".jpg"
  313. }
  314. //处理图片旋转的问题
  315. if imageOrientation != UIImage.Orientation.up {
  316. let newImage = UIImage(data: data)?.fixOrientation()
  317. if newImage != nil {
  318. newData = newImage!.pngData()!
  319. }
  320. }
  321. //处理图片旋转的问题
  322. if imageOrientation != UIImage.Orientation.up {
  323. let newImage = UIImage(data: data)?.fixOrientation()
  324. if newImage != nil {
  325. newData = newImage!.pngData()!
  326. }
  327. }
  328. callback(fName, newData)
  329. }
  330. break
  331. case .video:
  332. print("视频文件。还不支持。。。。。")
  333. break
  334. default :
  335. print("未知类型的文件。。。。。。")
  336. break
  337. }
  338. }
  339. }, completion: nil)
  340. }
  341. }
  342. //MARK: - Notification
  343. extension UIViewController {
  344. ///EZSE: Adds an NotificationCenter with name and Selector
  345. open func addNotificationObserver(_ name: String, selector: Selector) {
  346. NotificationCenter.default.addObserver(self, selector: selector, name: NSNotification.Name(rawValue: name), object: nil)
  347. }
  348. ///EZSE: Removes an NSNotificationCenter for name
  349. open func removeNotificationObserver(_ name: String) {
  350. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: name), object: nil)
  351. }
  352. ///EZSE: Removes NotificationCenter'd observer
  353. open func removeNotificationObserver() {
  354. NotificationCenter.default.removeObserver(self)
  355. }
  356. ///EZSE: Adds a NotificationCenter Observer for keyboardWillShowNotification()
  357. ///
  358. /// ⚠️ You also need to implement ```keyboardWillShowNotification(_ notification: Notification)```
  359. open func addKeyboardWillShowNotification() {
  360. self.addNotificationObserver(UIResponder.keyboardWillShowNotification.rawValue, selector: #selector(UIViewController.keyboardWillShowNotification(_:)))
  361. }
  362. ///EZSE: Adds a NotificationCenter Observer for keyboardDidShowNotification()
  363. ///
  364. /// ⚠️ You also need to implement ```keyboardDidShowNotification(_ notification: Notification)```
  365. public func addKeyboardDidShowNotification() {
  366. self.addNotificationObserver(UIResponder.keyboardDidShowNotification.rawValue, selector: #selector(UIViewController.keyboardDidShowNotification(_:)))
  367. }
  368. ///EZSE: Adds a NotificationCenter Observer for keyboardWillHideNotification()
  369. ///
  370. /// ⚠️ You also need to implement ```keyboardWillHideNotification(_ notification: Notification)```
  371. open func addKeyboardWillHideNotification() {
  372. self.addNotificationObserver(UIResponder.keyboardWillHideNotification.rawValue, selector: #selector(UIViewController.keyboardWillHideNotification(_:)))
  373. }
  374. ///EZSE: Adds a NotificationCenter Observer for keyboardDidHideNotification()
  375. ///
  376. /// ⚠️ You also need to implement ```keyboardDidHideNotification(_ notification: Notification)```
  377. open func addKeyboardDidHideNotification() {
  378. self.addNotificationObserver(UIResponder.keyboardDidHideNotification.rawValue, selector: #selector(UIViewController.keyboardDidHideNotification(_:)))
  379. }
  380. ///EZSE: Removes keyboardWillShowNotification()'s NotificationCenter Observer
  381. open func removeKeyboardWillShowNotification() {
  382. self.removeNotificationObserver(UIResponder.keyboardWillShowNotification.rawValue)
  383. }
  384. ///EZSE: Removes keyboardDidShowNotification()'s NotificationCenter Observer
  385. open func removeKeyboardDidShowNotification() {
  386. self.removeNotificationObserver(UIResponder.keyboardDidShowNotification.rawValue)
  387. }
  388. ///EZSE: Removes keyboardWillHideNotification()'s NotificationCenter Observer
  389. open func removeKeyboardWillHideNotification() {
  390. self.removeNotificationObserver(UIResponder.keyboardWillHideNotification.rawValue)
  391. }
  392. ///EZSE: Removes keyboardDidHideNotification()'s NotificationCenter Observer
  393. open func removeKeyboardDidHideNotification() {
  394. self.removeNotificationObserver(UIResponder.keyboardDidHideNotification.rawValue)
  395. }
  396. @objc open func keyboardDidShowNotification(_ notification: Notification) {
  397. if let nInfo = (notification as NSNotification).userInfo, let value = nInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
  398. let frame = value.cgRectValue
  399. keyboardDidShowWithFrame(frame)
  400. }
  401. }
  402. @objc open func keyboardWillShowNotification(_ notification: Notification) {
  403. if let nInfo = (notification as NSNotification).userInfo, let value = nInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
  404. let frame = value.cgRectValue
  405. keyboardWillShowWithFrame(frame)
  406. }
  407. }
  408. @objc open func keyboardWillHideNotification(_ notification: Notification) {
  409. if let nInfo = (notification as NSNotification).userInfo, let value = nInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
  410. let frame = value.cgRectValue
  411. keyboardWillHideWithFrame(frame)
  412. }
  413. }
  414. @objc open func keyboardDidHideNotification(_ notification: Notification) {
  415. if let nInfo = (notification as NSNotification).userInfo, let value = nInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
  416. let frame = value.cgRectValue
  417. keyboardDidHideWithFrame(frame)
  418. }
  419. }
  420. @objc open func keyboardWillShowWithFrame(_ frame: CGRect) {
  421. }
  422. @objc open func keyboardDidShowWithFrame(_ frame: CGRect) {
  423. }
  424. @objc open func keyboardWillHideWithFrame(_ frame: CGRect) {
  425. }
  426. @objc open func keyboardDidHideWithFrame(_ frame: CGRect) {
  427. }
  428. //EZSE: Makes the UIViewController register tap events and hides keyboard when clicked somewhere in the ViewController.
  429. open func hideKeyboardWhenTappedAround(cancelTouches: Bool = false) {
  430. let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
  431. tap.cancelsTouchesInView = cancelTouches
  432. view.addGestureRecognizer(tap)
  433. }
  434. //EZSE: Dismisses keyboard
  435. @objc open func dismissKeyboard() {
  436. view.endEditing(true)
  437. }
  438. ///跳转到应用设置页面
  439. ///
  440. /// - Parameter alertMessage: 确认提示消息,如果是nil就直接跳转
  441. func gotoApplicationSettings(alertMessage:String? = nil) {
  442. if alertMessage != nil {
  443. showDefaultConfirm(title: "提示", message: alertMessage!, okHandler: { (okAction) in
  444. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
  445. })
  446. }else {
  447. UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
  448. }
  449. }
  450. }