52af9ccbf95ea9f83d277c63cdd873776153545d
[mirrors/VoiceChatSdk_iOS.git] /
1 //
2 //  IQBarButtonItem.swift
3 //  https://github.com/hackiftekhar/IQKeyboardManager
4 //  Copyright (c) 2013-24 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
24 import UIKit
25
26 @available(iOSApplicationExtension, unavailable)
27 @MainActor
28 @objc open class IQBarButtonItem: UIBarButtonItem {
29
30     internal static let flexibleBarButtonItem: IQBarButtonItem = IQBarButtonItem(barButtonSystemItem: .flexibleSpace,
31                                                                                  target: nil, action: nil)
32
33     @objc public override init() {
34         super.init()
35         initialize()
36     }
37
38     @objc public required init?(coder aDecoder: NSCoder) {
39         super.init(coder: aDecoder)
40         initialize()
41     }
42
43     private func initialize() {
44
45         let states: [UIControl.State] = [.normal, .highlighted, .disabled, .focused]
46
47         for state in states {
48
49             setBackgroundImage(UIImage(), for: state, barMetrics: .default)
50             setBackgroundImage(UIImage(), for: state, style: .plain, barMetrics: .default)
51             setBackButtonBackgroundImage(UIImage(), for: state, barMetrics: .default)
52         }
53
54         setTitlePositionAdjustment(UIOffset(), for: .default)
55         setBackgroundVerticalPositionAdjustment(0, for: .default)
56         setBackButtonBackgroundVerticalPositionAdjustment(0, for: .default)
57     }
58
59     @objc override open var tintColor: UIColor? {
60         didSet {
61
62             var textAttributes: [NSAttributedString.Key: Any] = [:]
63             textAttributes[.foregroundColor] = tintColor
64
65             if let attributes: [NSAttributedString.Key: Any] = titleTextAttributes(for: .normal) {
66                 for (key, value) in attributes {
67                     textAttributes[key] = value
68                 }
69             }
70
71             setTitleTextAttributes(textAttributes, for: .normal)
72         }
73     }
74
75     /**
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
79      */
80     internal var isSystemItem: Bool = false
81
82     /**
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.
86      
87      @param target Target object.
88      @param action Target Selector.
89      */
90     @objc open func setTarget(_ target: AnyObject?, action: Selector?) {
91         if let target: AnyObject = target, let action: Selector = action {
92             invocation = IQInvocation(target, action)
93         } else {
94             invocation = nil
95         }
96     }
97
98     /**
99      Customized Invocation to be called when button is pressed.
100      invocation is internally created using setTarget:action: method.
101      */
102     @objc open var invocation: IQInvocation? {
103         didSet {
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 {
107
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)
112                 } else {
113                     titleBarButton.isEnabled = false
114                     titleBarButton.titleButton?.isEnabled = false
115                     titleBarButton.titleButton?.removeTarget(nil, action: nil, for: .touchUpInside)
116                 }
117             }
118         }
119     }
120 }