@@ -22,7 +22,9 @@ public class KeyboardAccessoryManager {
2222 private let keyHeight : CGFloat
2323 private let keyCornerRadius : CGFloat
2424 private let showDismissKeyboardKey : Bool
25+
2526 private weak var delegate : KeyboardAccessoryViewDelegate ?
27+ private var identifiedActionItems : [ String : Any ] = [ : ]
2628
2729 // MARK: - Initializer
2830
@@ -61,7 +63,7 @@ public class KeyboardAccessoryManager {
6163 if Self . isIPad {
6264 configure ( inputAssistantItem: textView. inputAssistantItem)
6365 } else {
64- textView. inputAccessoryView = makeInputView ( )
66+ textView. inputAccessoryView = inputAccessoryView
6567 }
6668 }
6769
@@ -73,56 +75,70 @@ public class KeyboardAccessoryManager {
7375 if Self . isIPad {
7476 configure ( inputAssistantItem: textField. inputAssistantItem)
7577 } else {
76- textField. inputAccessoryView = makeInputView ( )
78+ textField. inputAccessoryView = inputAccessoryView
7779 }
7880 }
7981
8082 /// Creates an instance of toolbar view that can be assigned to the text view's `inputAccessoryView`.
8183 /// - Returns: The keyboard accessory view instance.
82- public func makeInputView( ) -> KeyboardAccessoryView {
83- return KeyboardAccessoryView (
84- keyWidth: keyWidth,
85- keyHeight: keyHeight,
86- keyCornerRadius: keyCornerRadius,
87- keyMargin: keyMargin,
88- keyButtonGroups: keyButtonGroups,
89- showDismissKeyboardKey: showDismissKeyboardKey,
90- delegate: delegate)
91- }
84+ public private( set) lazy var inputAccessoryView = KeyboardAccessoryView (
85+ keyWidth: keyWidth,
86+ keyHeight: keyHeight,
87+ keyCornerRadius: keyCornerRadius,
88+ keyMargin: keyMargin,
89+ keyButtonGroups: keyButtonGroups,
90+ showDismissKeyboardKey: showDismissKeyboardKey,
91+ delegate: delegate)
9292
9393 /// Configures the `UITextInputAssistantItem` with given accessory manager.
9494 /// - Parameter inputAssistantItem: The `UITextInputAssistantItem` to be configured.
9595 public func configure( inputAssistantItem: UITextInputAssistantItem ) {
9696 var leadingButtons : [ UIBarButtonItem ] = [ ]
9797 var trailingButtons : [ UIBarButtonItem ] = [ ]
98- var overflowMenuActions : [ UIAction ] = [ ]
99-
100- for button in keyButtonGroups. flatMap ( { $0 } ) {
101- if button. position == . overflow {
102- guard let title = button. title else {
103- fatalError ( " [AccessoryKit] Overflow button must have a title " )
104- }
105- let action = UIAction (
106- title: title,
107- image: button. image,
108- handler: { handler in
109- button. tapHandler ? ( )
110- } )
111- overflowMenuActions. append ( action)
112- } else {
113- let buttonItem = UIBarButtonItem (
114- title: button. title,
115- image: button. image,
116- primaryAction: UIAction ( handler: { handler in
117- button. tapHandler ? ( )
118- } ) ,
119- menu: button. menu)
120- if button. position == . leading {
121- leadingButtons. append ( buttonItem)
98+ var overflowMenuActions : [ UIMenuElement ] = [ ]
99+
100+ for buttonGroup in keyButtonGroups {
101+ var groupOverflowActions : [ UIAction ] = [ ]
102+
103+ for button in buttonGroup {
104+ if button. position == . overflow {
105+ guard let title = button. title else {
106+ fatalError ( " [AccessoryKit] Overflow button must have a title " )
107+ }
108+ let action = UIAction (
109+ title: title,
110+ image: button. image,
111+ handler: { handler in
112+ button. tapHandler ? ( )
113+ } )
114+ groupOverflowActions. append ( action)
115+
116+ if let identifier = button. identifier {
117+ identifiedActionItems [ identifier] = action
118+ }
122119 } else {
123- trailingButtons. append ( buttonItem)
120+ let buttonItem = UIBarButtonItem (
121+ title: button. title,
122+ image: button. image,
123+ primaryAction: UIAction ( handler: { handler in
124+ button. tapHandler ? ( )
125+ } ) ,
126+ menu: button. menu)
127+ if button. position == . leading {
128+ leadingButtons. append ( buttonItem)
129+ } else {
130+ trailingButtons. append ( buttonItem)
131+ }
132+ if let identifier = button. identifier {
133+ identifiedActionItems [ identifier] = buttonItem
134+ }
124135 }
125136 }
137+
138+ if !groupOverflowActions. isEmpty {
139+ let groupedAction = UIMenu ( title: " " , options: . displayInline, children: groupOverflowActions)
140+ overflowMenuActions. append ( groupedAction)
141+ }
126142 }
127143
128144 if !overflowMenuActions. isEmpty {
@@ -158,6 +174,33 @@ public class KeyboardAccessoryManager {
158174 }
159175 }
160176
177+ // MARK: - API
178+
179+ /// Set `isEnabled` value on the key with a given identifier.
180+ /// - Parameters:
181+ /// - enabled: Boolean value indicating whether the key is enabled.
182+ /// - identifier: Identifier of menu item.
183+ public func setEnabled( _ enabled: Bool , for identifier: String ) {
184+ if Self . isIPad {
185+ if let item = identifiedActionItems [ identifier] {
186+ switch item {
187+ case is UIAction :
188+ if !enabled {
189+ ( item as? UIAction ) ? . attributes = . disabled
190+ } else {
191+ ( item as? UIAction ) ? . attributes = [ ]
192+ }
193+ case is UIBarButtonItem :
194+ ( item as? UIBarButtonItem ) ? . isEnabled = enabled
195+ default :
196+ break
197+ }
198+ }
199+ } else {
200+ inputAccessoryView. setEnabled ( enabled, for: identifier)
201+ }
202+ }
203+
161204 // MARK: - Private
162205
163206 @objc
0 commit comments