Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use function return ToolbarItem in SwiftUI?

Tags:

swift

swiftui

I am new to SwiftUI,I tried to put the extra code into the function, but I get some error message and I don't know how to fix:

'some' types are only implemented for the declared type of properties and subscripts and the return type of functions

My code:

func test() -> ToolbarItem<Void, some View> {
   return ToolbarItem(direction: .right) {
        Label("", systemImage: "icloud.and.arrow.up.fill")
            .foregroundColor(.white)
            .frame(width: itemWith, height: 30, alignment: .center)
   }
}

Does anyone know how to do this? thank you

like image 662
Karim Avatar asked Feb 02 '26 05:02

Karim


1 Answers

@ToolbarContentBuilder
func toolbars() -> some ToolbarContent {
    ToolbarItem(placement: .navigationBarLeading) {
        Button {
                
        } label: {
            Image(systemName: "chevron.left")
        }
    }
    
    ToolbarItem(placement: .navigationBarTrailing) {
        Button {
            
        } label: {
            Image(systemName: "camera.fill")
        }
    }
}

ToolbarContentBuilder Constructs a toolbar item set from multi-expression closures.

like image 114
chenxi Avatar answered Feb 04 '26 19:02

chenxi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!