Xcode 的 canvas 异步任务await BottomCustomPopup().present()不执行,要在 simulator 中才执行
struct PartnerProfileView: View {
@State private var showSheet: Bool = true
let partnerName: String
var body: some View {
ZStack(alignment: .bottom) {
Color("EFEFF4_0F2534")
ScrollView(showsIndicators: false) {
headerSection()
infoSection()
.padding(.bottom, 5)
sectionTitle("Other Skills")
skillsGrid()
sectionTitle("···")
dynamicsSection()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
CustomTabBar()
}
.navigationBarHidden(true)
.ignoresSafeArea(edges: .all)
.onAppear {
print("PartnerProfileView received")
Task {
await BottomCustomPopup().present()
}
}
.onChange(of: showSheet) { oldState, newValue in
print("oldState \(oldState)")
print("newValue \(newValue)")
}
}
}
//@MainActor
struct BottomCustomPopup: BottomPopup {
var body: some View {
HStack(spacing: 0) {
Text("Hello World")
Spacer()
Button(action: { Task { await dismissLastPopup() }}) { Text("Dismiss") }
}
.padding(.vertical, 20)
.padding(.leading, 24)
.padding(.trailing, 16)
}
}
使用条件编译来区分 Canvas 预览和实际运行环境。例如,你可以在 onAppear 中添加一个条件检查,确保异步任务只在非 Canvas 环境中执行:
或者