最近 Swift (iOS アプリ開発) を触っているわたしです。
タイトル通り、複数の要素を受け付ける View、つまりは ZStack とかのように、次のようなコードがかける View 要素を作りたい場合のやり方:
ZStack {
Color("BrandColor")
Image("BrandIcon")
.resizable()
.aspectRatio(contentMode: .fit)
.padding()
}
作り方は簡単!引数に @ViewBuilder 属性を付けるだけ!
import SwiftUI struct Tab<Content: View>: View { let content: () -> Content init(@ViewBuilder content: @escaping () -> Content) { self.content = content } var body: some View { content() } }
これで、例えば次のようなコードがかけるようになる:
Tab {
SomeView()
AnotherView()
}
これを見つけるのに結構悩んだので、将来の自分のためにメモを残す。