我在 xcode 上使用实时预览时遇到问题。该项目构建良好并在模拟器上运行。我已经连接了所有环境对象,所以我有点困惑。是的,我在应用程序文件本身上也有 EO。
“无法在此文件中预览 - 向代理发送显示消息的消息发送失败”
我在 TabBarView 中的每个视图中都使用 AlertSettingsView - HomeView、SearchView、ExploreView 和 OrdersView。使用 AlertSettings 预览提供程序时,一切正常。但是当我尝试预览整个应用程序时,它不再起作用。如果我删除代码下方突出显示的行,则预览效果会很好。
struct AlertSettingsView: View {
@EnvironmentObject var avm: AlertsViewModel
private let width: CGFloat = UIScreen.main.bounds.width
private let height: CGFloat = UIScreen.main.bounds.height
var body: some View {
ZStack {
Color.theme.orange.ignoresSafeArea()
VStack {
ForEach(0..<8) { text in
Text("lakjsdfiojas;dfkljadskl;jfj")
}
}
}
.frame(width: width, height: height/2)
.animation(.spring())
.cornerRadius(15)
.offset(y: avm.settingsViewOffset) <-- This line breaks the preview
}
}
class AlertsViewModel: ObservableObject {
@Published var showAlertsView: Bool = true
@Published var showSettingsView: Bool = false
@Published var settingsViewOffset: CGFloat = UIScreen.main.bounds.height/2
}
struct TabBarView: View {
@EnvironmentObject var tvm: TradeViewModel
@State private var selectedTab: Int = 0
var body: some View {
ZStack {
Color.theme.background.ignoresSafeArea()
VStack(spacing: -4) {
ZStack {
switch selectedTab {
case 0:
HomeView()
case 1:
SearchView()
case 2:
ExploreView()
case 3:
OrdersView()
default:
HomeView()
}
}
Spacer()
TabBarButtons(selectedTab: $selectedTab)
}
.ignoresSafeArea(.keyboard, edges: .bottom)
HomeMenuView()
}
.fullScreenCover(isPresented: $tvm.showTradeView) {
TradeView()
}
}
}
struct TabBarView_Previews: PreviewProvider {
@State static var avm = AlertsViewModel()
@State static var hvm = HomeViewModel()
@State static var ipvm = InvestorProfileViewModel()
@State static var apvm = ArtistProfileViewModel()
@State static var tvm = TradeViewModel()
static var previews: some View {
TabBarView()
.environmentObject(avm)
.environmentObject(hvm)
.environmentObject(ipvm)
.environmentObject(apvm)
.environmentObject(tvm)
.preferredColorScheme(.dark)
}
}