2 // IQBarButtonItem.swift
3 // https://github.com/hackiftekhar/IQKeyboardManager
4 // Copyright (c) 2013-24 Iftekhar Qurashi.
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:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
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
26 @available(iOSApplicationExtension, unavailable)
28 @objc open class IQBarButtonItem: UIBarButtonItem {
30 internal static let flexibleBarButtonItem: IQBarButtonItem = IQBarButtonItem(barButtonSystemItem: .flexibleSpace,
31 target: nil, action: nil)
33 @objc public override init() {
38 @objc public required init?(coder aDecoder: NSCoder) {
39 super.init(coder: aDecoder)
43 private func initialize() {
45 let states: [UIControl.State] = [.normal, .highlighted, .disabled, .focused]
49 setBackgroundImage(UIImage(), for: state, barMetrics: .default)
50 setBackgroundImage(UIImage(), for: state, style: .plain, barMetrics: .default)
51 setBackButtonBackgroundImage(UIImage(), for: state, barMetrics: .default)
54 setTitlePositionAdjustment(UIOffset(), for: .default)
55 setBackgroundVerticalPositionAdjustment(0, for: .default)
56 setBackButtonBackgroundVerticalPositionAdjustment(0, for: .default)
59 @objc override open var tintColor: UIColor? {
62 var textAttributes: [NSAttributedString.Key: Any] = [:]
63 textAttributes[.foregroundColor] = tintColor
65 if let attributes: [NSAttributedString.Key: Any] = titleTextAttributes(for: .normal) {
66 for (key, value) in attributes {
67 textAttributes[key] = value
71 setTitleTextAttributes(textAttributes, for: .normal)
76 Boolean to know if it's a system item or custom item,
77 we are having a limitation that we cannot override a designated initializer,
78 so we are manually setting this property once in initialization
80 internal var isSystemItem: Bool = false
83 Additional target & action to do get callback action.
84 Note that setting custom target & selector doesn't affect native functionality,
85 this is just an additional target to get a callback.
87 @param target Target object.
88 @param action Target Selector.
90 @objc open func setTarget(_ target: AnyObject?, action: Selector?) {
91 if let target: AnyObject = target, let action: Selector = action {
92 invocation = IQInvocation(target, action)
99 Customized Invocation to be called when button is pressed.
100 invocation is internally created using setTarget:action: method.
102 @objc open var invocation: IQInvocation? {
104 // We have to put this condition here because if we override this function then
105 // We were getting "Cannot override '_' which has been marked unavailable" in Xcode 15
106 if let titleBarButton = self as? IQTitleBarButtonItem {
108 if let target = invocation?.target, let action = invocation?.action {
109 titleBarButton.isEnabled = true
110 titleBarButton.titleButton?.isEnabled = true
111 titleBarButton.titleButton?.addTarget(target, action: action, for: .touchUpInside)
113 titleBarButton.isEnabled = false
114 titleBarButton.titleButton?.isEnabled = false
115 titleBarButton.titleButton?.removeTarget(nil, action: nil, for: .touchUpInside)