gpt4 book ai didi

SwiftUI:如何只为 iPhone 显示一些工具栏按钮

转载 作者:行者123 更新时间:2023-12-04 14:58:17 24 4
gpt4 key购买 nike

我有一组工具栏按钮,只有在设备是 iPhone(不是 iPad)时才应该显示。
以下代码失败并出现此错误:

Closure containing control flow statement cannot be used with result builder '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()
}
}
我很高兴创建一个单独的函数来实现它。我根本不知道该怎么做。

最佳答案

可能你想要这个

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

关于SwiftUI:如何只为 iPhone 显示一些工具栏按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67475447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com