我创建了一个“包装器”视图来处理一些标准商标,例如背景色等,它以View
作为参数,但是我不知道如何传递示例View
在PreviewProvider
中查看它是否可以在Xcode中使用。当我在模拟器中构建该包装视图时,该视图有效-我只是不知道如何预览。
import SwiftUI
struct BackgroundView<Content: View>: View {
let contentView: Content
init(@ViewBuilder content: @escaping () -> Content) {
self.contentView = content()
}
var body: some View {
ZStack {
GeometryReader { geo in
HStack {
Spacer()
Image("Background Watermark")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 0.7 * geo.size.width, height: geo.size.height, alignment: .topLeading)
}
}
VStack {
self.contentView
}
}
.background(Color("Purple"))
.edgesIgnoringSafeArea(.top)
}
}
// This is where I'm struggling
struct BackgroundView_Previews: PreviewProvider {
static var previews: some View {
BackgroundView(content: ... )
}
}
我尝试将简单的Text
传递给它,但得到Cannot convert value of type 'Text' to expected argument type '() -> Content'
static var previews: some View {
BackgroundView(content: Text("Hello, world"))
}
然后我尝试传递一个返回some View
的函数,认为关闭是问题所在,但我得到了Instance member 'sampleTextView' cannot be used on type 'BackgroundView_Previews'
:
func sampleTextView() -> some View {
Text("Hello world")
}
static var previews: some View {
BackgroundView(content: sampleTextView)
}
有什么主意我在做错什么,或者如何加载一个简单的Text
视图只是为了预览?
答案 0 :(得分:1)
预计会像
那样关闭sqlite3.exe CopyOfMM.DB "REINDEX Songs;"
或
static var previews: some View {
BackgroundView(content: { Text("Hello, world") })
}