This is a companion discussion topic for the original entry at https://www.raywenderlich.com/17314449-swiftui-layout-interfaces/lessons/3
This is a companion discussion topic for the original entry at https://www.raywenderlich.com/17314449-swiftui-layout-interfaces/lessons/3
Hello, one question.
Why every time I press a button from Toolbar menu the elements change? Is it because of the “randomElement”?
And I have another question, I was trying to extract the Toolbar
view so the .toolbar
modifier in the ContentView
struct could fit in one line. This is my attempt:
private struct GenreToolbar: View {
@Binding var selectedGenre: Genre?
var body: some View {
ToolbarItem {
Menu("Genre") {
ForEach(Genre.list) { genre in
Button(genre.name) { selectedGenre = genre }
}
}
}
}
}
This breaks with the following errors:
Return type of property 'body' requires that 'ToolbarItem<Void, Menu<Text, ForEach<[Genre], some Hashable, Button<Text>>>>' conform to 'View'
Static method 'buildBlock' requires that 'ToolbarItem<Void, Menu<Text, ForEach<[Genre], some Hashable, Button<Text>>>>' conform to 'View'
Any ideas?
Yes.
The toolbar
modifier doesn’t need a View
. It needs a ToolbarContent
. So just change both of these to that, and you’re set!
private struct GenreToolbar: View {
@Binding var selectedGenre: Genre?
var body: some View {