UIBarButtonItem+NXBadgeView.swift 4.3 KB

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