创建自己的容器视图。你可以用
@ViewBuilder
VStack
HStack
它需要一个充满子视图的内容闭包。
import SwiftUI
struct CustomContainer<Content: View>: View {
init(@ViewBuilder content: () -> Content) {
self.content = content()
}
let content: Content
var body: some View {
VStack(spacing: 0) {
content
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(hue: 0, saturation: 0.3, brightness: 1))
Toolbar()
}
}
}
struct Toolbar: View {
var body: some View {
HStack {
Spacer()
button("person.3.fill")
Spacer()
button("printer.fill")
Spacer()
button("heart.fill")
Spacer()
button("bubble.left.fill")
Spacer()
} //
.frame(height: 44)
}
private func button(_ name: String) -> some View {
Image(systemName: name)
.resizable()
.aspectRatio(contentMode: .fit)
.padding(4)
.frame(width: 40, height: 40)
}
}
下面是一个使用它的视图:
struct HelloView: View {
var body: some View {
CustomContainer {
VStack {
Text("hello")
Text("khawar")
}
}
}
}
import PlaygroundSupport
PlaygroundPage.current.setLiveView(HelloView())