@@ -3,6 +3,11 @@ import UIKit
33
44/// An item which can be displayed in a keyboard toolbar. Implementers of this do not have
55/// to implement ``SwiftCrossUI/View``.
6+ ///
7+ /// Toolbar items are expected to be "stateless". Mutations of `@State` properties of toolbar
8+ /// items will not cause the toolbar to be updated. The toolbar is only updated when the view
9+ /// containing the ``View/keyboardToolbar(animateChanges:body:)`` modifier is updated, so any
10+ /// state necessary for the toolbar should live in the view itself.
611public protocol ToolbarItem {
712 /// Convert the item to a `UIBarButtonItem`, which will be placed in the keyboard toolbar.
813 func asBarButtonItem( ) -> UIBarButtonItem
@@ -39,10 +44,6 @@ public enum ToolbarBuilder {
3944 public static func buildEither( second component: Component ) -> Component {
4045 component
4146 }
42-
43- public static func buildFinalResult( _ component: Component ) -> [ UIBarButtonItem ] {
44- component. map { $0. asBarButtonItem ( ) }
45- }
4647}
4748
4849final class CallbackBarButtonItem : UIBarButtonItem {
@@ -80,9 +81,9 @@ extension Spacer: ToolbarItem {
8081 if let minLength, minLength > 0 {
8182 print (
8283 """
83- Warning: Spacer's minLength is ignored inside keyboard toolbars, as \
84- flexible-length spacers cannot specify a minimum width . Use `.frame(width:)` \
85- for a fixed-length spacer .
84+ Warning: Spacer's minLength property is ignored within keyboard toolbars \
85+ due to UIKit limitations . Use `Spacer()` for unconstrained spacers and \
86+ `Spacer().frame(width: _)` for fixed-length spacers .
8687 """
8788 )
8889 }
@@ -168,11 +169,11 @@ extension View {
168169 /// - body: The toolbar's contents
169170 public func keyboardToolbar(
170171 animateChanges: Bool = true ,
171- @ToolbarBuilder body: @escaping ( ) -> [ UIBarButtonItem ]
172+ @ToolbarBuilder body: @escaping ( ) -> ToolbarBuilder . Component
172173 ) -> some View {
173174 EnvironmentModifier ( self ) { environment in
174175 environment. with ( \. updateToolbar) { toolbar in
175- toolbar. setItems ( body ( ) , animated: animateChanges)
176+ toolbar. setItems ( body ( ) . map { $0 . asBarButtonItem ( ) } , animated: animateChanges)
176177 toolbar. sizeToFit ( )
177178 }
178179 }
0 commit comments