SwiftUI制作View可嵌套组件
Posted liuxiaokun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SwiftUI制作View可嵌套组件相关的知识,希望对你有一定的参考价值。
SwiftUI制作View可嵌套组件
-
了解嵌套组件制造原理
-
制作一套嵌套组件
-
掌握配置嵌套组件的preview的方法
-
如何初始化嵌套参数
-
基础构造
struct ENavigationView<Content: View>: View {
let viewBuilder: () -> Content
var body: some View {
NavigationView {
VStack {
viewBuilder()
.navigationBarTitle("My App")
}
}
}
}
struct ENavigationView_Previews: PreviewProvider {
static var previews: some View {
ENavigationView {
Text("Preview")
}
}
}
struct ContentView: View {
var body: some View {
ENavigationView {
Text("My Text")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
- 初始化参数
struct ENavigationView<Content: View>: View {
let viewBuilder: () -> Content
@Binding var oneFlag:Int
init(oneFlag:Int,, @ViewBuilder content: @escaping () -> Content)
{
self.oneFlag = oneFlag
self.viewBuilder = content
}
var body: some View {
NavigationView {
VStack {
viewBuilder()
.navigationBarTitle("My App")
}
}
}
}
struct ENavigationView_Previews: PreviewProvider {
static var previews: some View {
ENavigationView {
Text("Preview")
}
}
}
struct ContentView: View {
@State var oneFlag = 1
var body: some View {
ENavigationView(oneFlag:self.oneFlag) {
Text("My Text")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
以上是关于SwiftUI制作View可嵌套组件的主要内容,如果未能解决你的问题,请参考以下文章