模态视图在 IOS 14 中显示为空
modal view showing up empty in IOS 14
在 iOS 14 上将项目更新为 swiftUI 2.0 并呈现模态视图似乎已损坏。
模态视图出现empty.
这是我使用的代码和我创建的示例项目来说明这一点:
https://github.com/Esowes/testModal
内容视图:
import SwiftUI
import CoreData
struct ContentView: View {
@State var modalIsPresented = false // The "settingsView" modally presented as a sheet
@State private var modalViewCaller = 0 // This triggers the appropriate modal (only one in this example)
var body: some View {
NavigationView {
VStack {
Spacer()
Button(action: {
self.modalViewCaller = 1 // SettingsView
self.modalIsPresented = true
}
) {
Text("Tap to present Modal")
}
Spacer()
} // END of main VStack
.onAppear() {
self.modalViewCaller = 0
}
.navigationBarTitle("Test app", displayMode: .inline)
} // END of NavigationView
.sheet(isPresented: $modalIsPresented, content: sheetContent)
.navigationViewStyle(StackNavigationViewStyle()) // This avoids dual column on iPad
} // END of var body: some View
// MARK: @ViewBuilder func sheetContent() :
@ViewBuilder func sheetContent() -> some View {
if modalViewCaller == 1 {
SettingsView()
.navigationViewStyle(StackNavigationViewStyle())
.onDisappear { // This always triggered
print("onDissappear triggered ! at \(Date().debugDescription)")
self.modalViewCaller = 0
}
}
} // END of func sheetContent
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
return ContentView()
}
}
如果有人有想法,我将不胜感激,因为到目前为止我的搜索没有任何结果...
谢谢
可能的解决方案是将 sheet 的内容分离到专用的 sub-view 中,如下所示。
测试 Xcode 12.1 / iOS 14.1
struct ContentView: View {
@State var modalIsPresented = false // The "settingsView" modally presented as a sheet
@State private var modalViewCaller = 0 // This triggers the appropriate modal (only one in this example)
var body: some View {
NavigationView {
VStack {
Spacer()
Button(action: {
self.modalViewCaller = 1 // SettingsView
self.modalIsPresented = true
}
) {
Text("Tap to present Modal")
}
Spacer()
} // END of main VStack
.onAppear() {
self.modalViewCaller = 0
}
.navigationBarTitle("Test app", displayMode: .inline)
} // END of NavigationView
.sheet(isPresented: $modalIsPresented) {
SheetContent(modalViewCaller: $modalViewCaller) // << here !!
}
.navigationViewStyle(StackNavigationViewStyle()) // This avoids dual column on iPad
} // END of var body: some View
// MARK: @ViewBuilder func sheetContent() :
}
struct SheetContent: View {
@Binding var modalViewCaller: Int
var body: some View {
if modalViewCaller == 1 {
SettingsView()
.navigationViewStyle(StackNavigationViewStyle())
.onDisappear { // This always triggered
print("onDissappear triggered ! at \(Date().debugDescription)")
self.modalViewCaller = 0
}
}
}
}
在 iOS 14 上将项目更新为 swiftUI 2.0 并呈现模态视图似乎已损坏。 模态视图出现empty.
这是我使用的代码和我创建的示例项目来说明这一点: https://github.com/Esowes/testModal
内容视图:
import SwiftUI
import CoreData
struct ContentView: View {
@State var modalIsPresented = false // The "settingsView" modally presented as a sheet
@State private var modalViewCaller = 0 // This triggers the appropriate modal (only one in this example)
var body: some View {
NavigationView {
VStack {
Spacer()
Button(action: {
self.modalViewCaller = 1 // SettingsView
self.modalIsPresented = true
}
) {
Text("Tap to present Modal")
}
Spacer()
} // END of main VStack
.onAppear() {
self.modalViewCaller = 0
}
.navigationBarTitle("Test app", displayMode: .inline)
} // END of NavigationView
.sheet(isPresented: $modalIsPresented, content: sheetContent)
.navigationViewStyle(StackNavigationViewStyle()) // This avoids dual column on iPad
} // END of var body: some View
// MARK: @ViewBuilder func sheetContent() :
@ViewBuilder func sheetContent() -> some View {
if modalViewCaller == 1 {
SettingsView()
.navigationViewStyle(StackNavigationViewStyle())
.onDisappear { // This always triggered
print("onDissappear triggered ! at \(Date().debugDescription)")
self.modalViewCaller = 0
}
}
} // END of func sheetContent
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
return ContentView()
}
}
如果有人有想法,我将不胜感激,因为到目前为止我的搜索没有任何结果...
谢谢
可能的解决方案是将 sheet 的内容分离到专用的 sub-view 中,如下所示。
测试 Xcode 12.1 / iOS 14.1
struct ContentView: View {
@State var modalIsPresented = false // The "settingsView" modally presented as a sheet
@State private var modalViewCaller = 0 // This triggers the appropriate modal (only one in this example)
var body: some View {
NavigationView {
VStack {
Spacer()
Button(action: {
self.modalViewCaller = 1 // SettingsView
self.modalIsPresented = true
}
) {
Text("Tap to present Modal")
}
Spacer()
} // END of main VStack
.onAppear() {
self.modalViewCaller = 0
}
.navigationBarTitle("Test app", displayMode: .inline)
} // END of NavigationView
.sheet(isPresented: $modalIsPresented) {
SheetContent(modalViewCaller: $modalViewCaller) // << here !!
}
.navigationViewStyle(StackNavigationViewStyle()) // This avoids dual column on iPad
} // END of var body: some View
// MARK: @ViewBuilder func sheetContent() :
}
struct SheetContent: View {
@Binding var modalViewCaller: Int
var body: some View {
if modalViewCaller == 1 {
SettingsView()
.navigationViewStyle(StackNavigationViewStyle())
.onDisappear { // This always triggered
print("onDissappear triggered ! at \(Date().debugDescription)")
self.modalViewCaller = 0
}
}
}
}