如何在SwiftUI中使用函数返回的ToolbarItem?

6

我刚接触SwiftUI,我试图将额外的代码放入函数中,但是出现了一些错误信息,我不知道如何修复:

'some'类型仅适用于属性和下标的声明类型以及函数的返回类型

我的代码:

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)
   }
}

有人知道如何做吗?谢谢

2个回答

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

ToolbarContentBuilder 构建由多个表达式闭包组成的工具栏项集合。


10
func test() -> some ToolbarContent {
    ToolbarItem(placement: ToolbarItemPlacement.navigationBarTrailing) {
        Label("", systemImage: "icloud.and.arrow.up.fill")
            .foregroundColor(.white)
            .frame(width: itemWith, height: 30, alignment: .center)
    }
}

这就是您要查找的内容。请注意使用 Toolbar(placement:) 替代您使用的 Toolbar(direction:)。我认为 direction 不是一个有效的参数。
作为解释,您需要使用协议 ToolbarContent 在全局函数中返回具有通用类型参数的工具栏内容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接