如何预览需要PresentationMode才能构建的此按钮?
在包含该按钮的主视图中,通过声明一个环境PresentationMode对象来创建它,该按钮可以正常工作:
@Environment(\.presentationMode) var presentationMode:Binding<PresentationMode>
struct BackButton: View {
@Binding var presentationMode: PresentationMode
var color: Color
var body: some View {
Button(action: {
self.$presentationMode.wrappedValue.dismiss()
}, label: { Image(systemName: "chevron.left")
.scaleEffect(1.3)
.foregroundColor(color)
.offset(x: -17)
.frame(width: 43, height: 43)
}
)
}
}
struct BackButton_Previews: PreviewProvider {
static var previews: some View {
let pres = PresentationMode()
return BackButton(presentationMode: pres, color: .black) // Compiler Error: PresentationMode cannot be constructed because it has no accessible initializers
}
}