SwiftUI: как показать некоторые кнопки панели инструментов только для iPhone
У меня есть набор кнопок на панели инструментов, которые должны отображаться, только если устройство 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()
}
}
Я счастлив создать для этого отдельную функцию. Я просто не могу понять, как это сделать.
1 ответ
Наверное, ты хотел этого
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")
})
}
}
}
}