IQBarButtonItem.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // IQBarButtonItem.swift
  3. // https://github.com/hackiftekhar/IQKeyboardManager
  4. // Copyright (c) 2013-16 Iftekhar Qurashi.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. import UIKit
  24. import Foundation
  25. open class IQBarButtonItem: UIBarButtonItem {
  26. private static var _classInitialize: Void = classInitialize()
  27. @objc public override init() {
  28. _ = IQBarButtonItem._classInitialize
  29. super.init()
  30. }
  31. @objc public required init?(coder aDecoder: NSCoder) {
  32. _ = IQBarButtonItem._classInitialize
  33. super.init(coder: aDecoder)
  34. }
  35. private class func classInitialize() {
  36. let appearanceProxy = self.appearance()
  37. #if swift(>=4.2)
  38. let states: [UIControl.State]
  39. #else
  40. let states: [UIControlState]
  41. #endif
  42. states = [.normal, .highlighted, .disabled, .selected, .application, .reserved]
  43. for state in states {
  44. appearanceProxy.setBackgroundImage(nil, for: state, barMetrics: .default)
  45. appearanceProxy.setBackgroundImage(nil, for: state, style: .done, barMetrics: .default)
  46. appearanceProxy.setBackgroundImage(nil, for: state, style: .plain, barMetrics: .default)
  47. appearanceProxy.setBackButtonBackgroundImage(nil, for: state, barMetrics: .default)
  48. }
  49. appearanceProxy.setTitlePositionAdjustment(UIOffset(), for: .default)
  50. appearanceProxy.setBackgroundVerticalPositionAdjustment(0, for: .default)
  51. appearanceProxy.setBackButtonBackgroundVerticalPositionAdjustment(0, for: .default)
  52. }
  53. @objc override open var tintColor: UIColor? {
  54. didSet {
  55. #if swift(>=4.2)
  56. var textAttributes = [NSAttributedString.Key: Any]()
  57. let foregroundColorKey = NSAttributedString.Key.foregroundColor
  58. #elseif swift(>=4)
  59. var textAttributes = [NSAttributedStringKey: Any]()
  60. let foregroundColorKey = NSAttributedStringKey.foregroundColor
  61. #else
  62. var textAttributes = [String: Any]()
  63. let foregroundColorKey = NSForegroundColorAttributeName
  64. #endif
  65. textAttributes[foregroundColorKey] = tintColor
  66. #if swift(>=4)
  67. if let attributes = titleTextAttributes(for: .normal) {
  68. for (key, value) in attributes {
  69. #if swift(>=4.2)
  70. textAttributes[key] = value
  71. #else
  72. textAttributes[NSAttributedStringKey.init(key)] = value
  73. #endif
  74. }
  75. }
  76. #else
  77. if let attributes = titleTextAttributes(for: .normal) {
  78. textAttributes = attributes
  79. }
  80. #endif
  81. setTitleTextAttributes(textAttributes, for: .normal)
  82. }
  83. }
  84. /**
  85. Boolean to know if it's a system item or custom item, we are having a limitation that we cannot override a designated initializer, so we are manually setting this property once in initialization
  86. */
  87. @objc internal var isSystemItem = false
  88. /**
  89. Additional target & action to do get callback action. Note that setting custom target & selector doesn't affect native functionality, this is just an additional target to get a callback.
  90. @param target Target object.
  91. @param action Target Selector.
  92. */
  93. @objc open func setTarget(_ target: AnyObject?, action: Selector?) {
  94. if let target = target, let action = action {
  95. invocation = IQInvocation(target, action)
  96. } else {
  97. invocation = nil
  98. }
  99. }
  100. /**
  101. Customized Invocation to be called when button is pressed. invocation is internally created using setTarget:action: method.
  102. */
  103. @objc open var invocation: IQInvocation?
  104. deinit {
  105. target = nil
  106. invocation = nil
  107. }
  108. }