ESTabBarController.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // ESTabBarController.swift
  3. //
  4. // Created by Vincent Li on 2017/2/8.
  5. // Copyright (c) 2013-2018 ESTabBarController (https://github.com/eggswift/ESTabBarController)
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. import UIKit
  26. /// 是否需要自定义点击事件回调类型
  27. public typealias ESTabBarControllerShouldHijackHandler = ((_ tabBarController: UITabBarController, _ viewController: UIViewController, _ index: Int) -> (Bool))
  28. /// 自定义点击事件回调类型
  29. public typealias ESTabBarControllerDidHijackHandler = ((_ tabBarController: UITabBarController, _ viewController: UIViewController, _ index: Int) -> (Void))
  30. open class ESTabBarController: UITabBarController, ESTabBarDelegate {
  31. /// 打印异常
  32. public static func printError(_ description: String) {
  33. #if DEBUG
  34. print("ERROR: ESTabBarController catch an error '\(description)' \n")
  35. #endif
  36. }
  37. /// 当前tabBarController是否存在"More"tab
  38. public static func isShowingMore(_ tabBarController: UITabBarController?) -> Bool {
  39. return tabBarController?.moreNavigationController.parent != nil
  40. }
  41. /// Ignore next selection or not.
  42. fileprivate var ignoreNextSelection = false
  43. /// Should hijack select action or not.
  44. open var shouldHijackHandler: ESTabBarControllerShouldHijackHandler?
  45. /// Hijack select action.
  46. open var didHijackHandler: ESTabBarControllerDidHijackHandler?
  47. /// Observer tabBarController's selectedViewController. change its selection when it will-set.
  48. open override var selectedViewController: UIViewController? {
  49. willSet {
  50. guard let newValue = newValue else {
  51. // if newValue == nil ...
  52. return
  53. }
  54. guard !ignoreNextSelection else {
  55. ignoreNextSelection = false
  56. return
  57. }
  58. guard let tabBar = self.tabBar as? ESTabBar, let items = tabBar.items, let index = viewControllers?.firstIndex(of: newValue) else {
  59. return
  60. }
  61. let value = (ESTabBarController.isShowingMore(self) && index > items.count - 1) ? items.count - 1 : index
  62. tabBar.select(itemAtIndex: value, animated: false)
  63. }
  64. }
  65. /// Observer tabBarController's selectedIndex. change its selection when it will-set.
  66. open override var selectedIndex: Int {
  67. willSet {
  68. guard !ignoreNextSelection else {
  69. ignoreNextSelection = false
  70. return
  71. }
  72. guard let tabBar = self.tabBar as? ESTabBar, let items = tabBar.items else {
  73. return
  74. }
  75. let value = (ESTabBarController.isShowingMore(self) && newValue > items.count - 1) ? items.count - 1 : newValue
  76. tabBar.select(itemAtIndex: value, animated: false)
  77. }
  78. }
  79. /// Customize set tabBar use KVC.
  80. open override func viewDidLoad() {
  81. super.viewDidLoad()
  82. let tabBar = { () -> ESTabBar in
  83. let tabBar = ESTabBar()
  84. tabBar.delegate = self
  85. tabBar.customDelegate = self
  86. tabBar.tabBarController = self
  87. return tabBar
  88. }()
  89. self.setValue(tabBar, forKey: "tabBar")
  90. }
  91. // MARK: - UITabBar delegate
  92. open override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
  93. guard let idx = tabBar.items?.firstIndex(of: item) else {
  94. return;
  95. }
  96. if idx == tabBar.items!.count - 1, ESTabBarController.isShowingMore(self) {
  97. ignoreNextSelection = true
  98. selectedViewController = moreNavigationController
  99. return;
  100. }
  101. if let vc = viewControllers?[idx] {
  102. ignoreNextSelection = true
  103. selectedIndex = idx
  104. delegate?.tabBarController?(self, didSelect: vc)
  105. }
  106. }
  107. open override func tabBar(_ tabBar: UITabBar, willBeginCustomizing items: [UITabBarItem]) {
  108. if let tabBar = tabBar as? ESTabBar {
  109. tabBar.updateLayout()
  110. }
  111. }
  112. open override func tabBar(_ tabBar: UITabBar, didEndCustomizing items: [UITabBarItem], changed: Bool) {
  113. if let tabBar = tabBar as? ESTabBar {
  114. tabBar.updateLayout()
  115. }
  116. }
  117. // MARK: - ESTabBar delegate
  118. internal func tabBar(_ tabBar: UITabBar, shouldSelect item: UITabBarItem) -> Bool {
  119. if let idx = tabBar.items?.firstIndex(of: item), let vc = viewControllers?[idx] {
  120. return delegate?.tabBarController?(self, shouldSelect: vc) ?? true
  121. }
  122. return true
  123. }
  124. internal func tabBar(_ tabBar: UITabBar, shouldHijack item: UITabBarItem) -> Bool {
  125. if let idx = tabBar.items?.firstIndex(of: item), let vc = viewControllers?[idx] {
  126. return shouldHijackHandler?(self, vc, idx) ?? false
  127. }
  128. return false
  129. }
  130. internal func tabBar(_ tabBar: UITabBar, didHijack item: UITabBarItem) {
  131. if let idx = tabBar.items?.firstIndex(of: item), let vc = viewControllers?[idx] {
  132. didHijackHandler?(self, vc, idx)
  133. }
  134. }
  135. }