UIViewController+Extension.swift 21 KB

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