Sat*_*ima 4 toolbaritems swiftui swiftui-view
我有一组工具栏按钮,仅当设备是 iPhone(不是 iPad)时才应显示。
以下代码失败并出现此错误:
包含控制流语句的闭包不能与结果生成器“ToolbarContentBuilder”一起使用
我确实明白为什么它会失败,但我无法提出一个解决方案来实现我所需要的。
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
List {
NavigationLink(
destination: Hello(),
label: {
Text("Hello")
})
}
}
}
}
struct Hello: View {
var body: some View {
Text("Hello World")
.toolbar() {
if UIDevice.current.userInterfaceIdiom == .phone {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
// do something
}, label: {
Text("Button1")
})
}
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
// do something
}, label: {
Text("Button2")
})
}
}
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
// do something
}, label: {
Text("Button3")
})
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Run Code Online (Sandbox Code Playgroud)
我很高兴创建一个单独的函数来实现它。我根本不知道该怎么做。
也许你想要这个
struct Hello: View {
var body: some View {
Text("Hello World")
.toolbar() {
ToolbarItemGroup(placement: .navigationBarTrailing) {
if UIDevice.current.userInterfaceIdiom == .phone {
Button(action: {
// do something
}, label: {
Text("Button1")
})
Button(action: {
// do something
}, label: {
Text("Button2")
})
}
Button(action: {
// do something
}, label: {
Text("Button3")
})
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
772 次 |
| 最近记录: |