196e90d604d73a481e0728cc59e7a8f72a315eca
[mirrors/VoiceChatSdk_iOS.git] /
1 //
2 //  IQToolbar.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 /** @abstract   IQToolbar for IQKeyboardManager.    */
27 @available(iOSApplicationExtension, unavailable)
28 @MainActor
29 @objc open class IQToolbar: UIToolbar, UIInputViewAudioFeedback {
30
31     override init(frame: CGRect) {
32         super.init(frame: frame)
33
34         initialize()
35     }
36
37     @objc required public init?(coder aDecoder: NSCoder) {
38         super.init(coder: aDecoder)
39
40         initialize()
41     }
42
43     private func initialize() {
44
45         sizeToFit()
46
47         autoresizingMask = .flexibleWidth
48         self.isTranslucent = true
49         self.barTintColor = nil
50
51         let positions: [UIBarPosition] = [.any, .bottom, .top, .topAttached]
52
53         for position in positions {
54
55             self.setBackgroundImage(nil, forToolbarPosition: position, barMetrics: .default)
56             self.setShadowImage(nil, forToolbarPosition: .any)
57         }
58
59         // Background color
60         self.backgroundColor = nil
61     }
62
63     /**
64      Additional bar buttons to show at the leading side.
65      */
66     open var additionalLeadingItems: [UIBarButtonItem] = []
67
68     /**
69      Additional bar buttons to show at the trailing side.
70      */
71     open var additionalTrailingItems: [UIBarButtonItem] = []
72
73     /**
74      Previous bar button of toolbar.
75      */
76     private var privatePreviousBarButton: IQBarButtonItem?
77     @objc open var previousBarButton: IQBarButtonItem {
78         get {
79             if privatePreviousBarButton == nil {
80                 privatePreviousBarButton = IQBarButtonItem(image: nil, style: .plain, target: nil, action: nil)
81             }
82             return privatePreviousBarButton!
83         }
84
85         set (newValue) {
86             privatePreviousBarButton = newValue
87         }
88     }
89
90     /**
91      Next bar button of toolbar.
92      */
93     private var privateNextBarButton: IQBarButtonItem?
94     @objc open var nextBarButton: IQBarButtonItem {
95         get {
96             if privateNextBarButton == nil {
97                 privateNextBarButton = IQBarButtonItem(image: nil, style: .plain, target: nil, action: nil)
98             }
99             return privateNextBarButton!
100         }
101
102         set (newValue) {
103             privateNextBarButton = newValue
104         }
105     }
106
107     /**
108      Title bar button of toolbar.
109      */
110     private var privateTitleBarButton: IQTitleBarButtonItem?
111     @objc open var titleBarButton: IQTitleBarButtonItem {
112         get {
113             if privateTitleBarButton == nil {
114                 privateTitleBarButton = IQTitleBarButtonItem(title: nil)
115                 privateTitleBarButton?.accessibilityTraits = .staticText
116                 privateTitleBarButton?.accessibilityLabel = "Title"
117                 privateTitleBarButton?.accessibilityIdentifier = privateTitleBarButton?.accessibilityLabel
118             }
119             return privateTitleBarButton!
120         }
121
122         set (newValue) {
123             privateTitleBarButton = newValue
124         }
125     }
126
127     /**
128      Done bar button of toolbar.
129      */
130     private var privateDoneBarButton: IQBarButtonItem?
131     @objc open var doneBarButton: IQBarButtonItem {
132         get {
133             if privateDoneBarButton == nil {
134                 privateDoneBarButton = IQBarButtonItem(title: nil, style: .done, target: nil, action: nil)
135             }
136             return privateDoneBarButton!
137         }
138
139         set (newValue) {
140             privateDoneBarButton = newValue
141         }
142     }
143
144     /**
145      Fixed space bar button of toolbar.
146      */
147     private var privateFixedSpaceBarButton: IQBarButtonItem?
148     @objc open var fixedSpaceBarButton: IQBarButtonItem {
149         get {
150             if privateFixedSpaceBarButton == nil {
151                 privateFixedSpaceBarButton = IQBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
152             }
153             privateFixedSpaceBarButton?.isSystemItem = true
154             privateFixedSpaceBarButton?.width = 6
155
156             return privateFixedSpaceBarButton!
157         }
158
159         set (newValue) {
160             privateFixedSpaceBarButton = newValue
161         }
162     }
163
164     @objc override open func sizeThatFits(_ size: CGSize) -> CGSize {
165         var sizeThatFit: CGSize = super.sizeThatFits(size)
166         sizeThatFit.height = 44
167         return sizeThatFit
168     }
169
170     @objc override open var tintColor: UIColor! {
171
172         didSet {
173             if let unwrappedItems: [UIBarButtonItem] = items {
174                 for item in unwrappedItems {
175                     item.tintColor = tintColor
176                 }
177             }
178         }
179     }
180
181     @objc open var enableInputClicksWhenVisible: Bool {
182         return true
183     }
184 }