[SwiftUI] HStack, VStack, ZStackで11以上のViewを並べる方法
Ambiguous reference to member 'buildBlock()'
SwiftUIのHStack, VStack, ZStackで11以上のViewを並べようとすると上記のエラーがでます。原因
原因はHStack, VStack, ZStackの実装方法が11以上のViewを引数として取れないようになっているからです。解決策
Groupを使ってViewをまとめると11以上並べることができます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct ContentView: View { | |
var body: some View { | |
ScrollView(.horizontal, showsIndicators: false) { | |
HStack { | |
Group { | |
EmojiTile(text: "😀", color: .blue) | |
EmojiTile(text: "😉", color: .green) | |
EmojiTile(text: "😊", color: .purple) | |
EmojiTile(text: "😀", color: .pink) | |
EmojiTile(text: "😉", color: .orange) | |
} | |
Group { | |
EmojiTile(text: "😀", color: .blue) | |
EmojiTile(text: "😉", color: .green) | |
EmojiTile(text: "😊", color: .purple) | |
EmojiTile(text: "😀", color: .pink) | |
EmojiTile(text: "😉", color: .orange) | |
} | |
Group { | |
EmojiTile(text: "😀", color: .blue) | |
EmojiTile(text: "😉", color: .green) | |
EmojiTile(text: "😊", color: .purple) | |
EmojiTile(text: "😀", color: .pink) | |
EmojiTile(text: "😉", color: .orange) | |
} | |
} | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} | |
struct EmojiTile: View { | |
var text: String | |
var color: Color | |
var body: some View { | |
Text(text) | |
.font(.custom("Emoji", size: 20)) | |
.padding(20) | |
.background(color) | |
.cornerRadius(10) | |
} | |
} |
SwiftUI for Absolute Beginners: Program Controls and Views for iPhone, iPad, and Mac Apps
posted with amazlet at 19.10.10
Jayant Varma
APress
売り上げランキング: 33,216
APress
売り上げランキング: 33,216