UITabBarItem+NXBadgeView.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // UITabBarItem+NXBadgeView.swift
  3. // NXFramework-Swift
  4. //
  5. // Created by ak on 2020/11/11.
  6. //
  7. import Foundation
  8. public extension NX where Base: UITabBarItem {
  9. var badgeView: NXBadgeControl {
  10. return _bottomView.nx.badgeView
  11. }
  12. /// 添加带文本内容的Badge, 默认右上角, 红色, 18pts
  13. ///
  14. /// Add Badge with text content, the default upper right corner, red backgroundColor, 18pts
  15. ///
  16. /// - Parameter text: 文本字符串
  17. func addBadge(text: String) {
  18. _bottomView.nx.addBadge(text: text)
  19. _bottomView.nx.moveBadge(x: 4, y: 3)
  20. }
  21. /// 添加带数字的Badge, 默认右上角,红色,18pts
  22. ///
  23. /// Add the Badge with numbers, the default upper right corner, red backgroundColor, 18pts
  24. ///
  25. /// - Parameter number: 整形数字
  26. func addBadge(number: Int) {
  27. _bottomView.nx.addBadge(number: number)
  28. _bottomView.nx.moveBadge(x: 4, y: 3)
  29. }
  30. /// 添加带颜色的小圆点, 默认右上角, 红色, 8pts
  31. ///
  32. /// Add small dots with color, the default upper right corner, red backgroundColor, 8pts
  33. ///
  34. /// - Parameter color: 颜色
  35. func addDot(color: UIColor?) {
  36. _bottomView.nx.addDot(color: color)
  37. }
  38. /// 设置Badge的偏移量, Badge中心点默认为其父视图的右上角
  39. ///
  40. /// Set Badge offset, Badge center point defaults to the top right corner of its parent view
  41. ///
  42. /// - Parameters:
  43. /// - x: X轴偏移量 (x<0: 左移, x>0: 右移) axis offset (x <0: left, x> 0: right)
  44. /// - y: Y轴偏移量 (y<0: 上移, y>0: 下移) axis offset (Y <0: up, y> 0: down)
  45. func moveBadge(x: CGFloat, y: CGFloat) {
  46. _bottomView.nx.moveBadge(x: x, y: y)
  47. }
  48. /// 设置Badge伸缩的方向
  49. ///
  50. /// Setting the direction of Badge expansion
  51. ///
  52. /// PPBadgeViewFlexModeHead, 左伸缩 Head Flex : <==●
  53. /// PPBadgeViewFlexModeTail, 右伸缩 Tail Flex : ●==>
  54. /// PPBadgeViewFlexModeMiddle 左右伸缩 Middle Flex : <=●=>
  55. /// - Parameter flexMode : Default is PPBadgeViewFlexModeTail
  56. func setBadge(flexMode: NXBadgeViewFlexMode = .tail) {
  57. _bottomView.nx.setBadge(flexMode: flexMode)
  58. }
  59. /// 设置Badge的高度,因为Badge宽度是动态可变的,通过改变Badge高度,其宽度也按比例变化,方便布局
  60. ///
  61. /// (注意: 此方法需要将Badge添加到控件上后再调用!!!)
  62. ///
  63. /// Set the height of Badge, because the Badge width is dynamically and  variable.By changing the Badge height in proportion to facilitate the layout.
  64. ///
  65. /// (Note: this method needs to add Badge to the controls and then use it !!!)
  66. ///
  67. /// - Parameter height: 高度大小
  68. func setBadge(height: CGFloat) {
  69. _bottomView.nx.setBadge(height: height)
  70. }
  71. /// 显示Badge
  72. func showBadge() {
  73. _bottomView.nx.showBadge()
  74. }
  75. /// 隐藏Badge
  76. func hiddenBadge() {
  77. _bottomView.nx.hiddenBadge()
  78. }
  79. // MARK: - 数字增加/减少, 注意:以下方法只适用于Badge内容为纯数字的情况
  80. // MARK: - Digital increase /decrease, note: the following method applies only to cases where the Badge content is purely numeric
  81. /// badge数字加1
  82. func increase() {
  83. _bottomView.nx.increase()
  84. }
  85. /// badge数字加number
  86. func increaseBy(number: Int) {
  87. _bottomView.nx.increaseBy(number: number)
  88. }
  89. /// badge数字加1
  90. func decrease() {
  91. _bottomView.nx.decrease()
  92. }
  93. /// badge数字减number
  94. func decreaseBy(number: Int) {
  95. _bottomView.nx.decreaseBy(number: number)
  96. }
  97. /// 通过Xcode视图调试工具找到UITabBarItem原生Badge所在父视图
  98. private var _bottomView: UIView {
  99. let tabBarButton = (self.base.value(forKey: "_view") as? UIView) ?? UIView()
  100. for subView in tabBarButton.subviews {
  101. guard let superclass = subView.superclass else { return tabBarButton }
  102. if superclass == NSClassFromString("UIImageView") {
  103. return subView
  104. }
  105. }
  106. return tabBarButton
  107. }
  108. }