关闭 .fullScreenCover 或 .sheet 后的 SwiftUI Picker 问题
SwiftUI Picker problem after dismissing .fullScreenCover or .sheet
我有一个选择器在显示和关闭 fullScreenCover 或 sheet 之前工作正常。有谁知道这个示例代码有什么问题,或者有解决方法吗?
我也尝试使用 self.presentation.wrappedValue.dismiss() 关闭 sheet,但结果相同。
示例 gif:https://i.stack.imgur.com/zmcmv.gif
代码:
import SwiftUI
struct ContentView: View {
@State var selectedFilterStatus = ActiveStatus.active
@State var showDetail = false
var body: some View {
NavigationView {
VStack {
Button(action: {
showDetail.toggle()
}, label: {
Text("Detail popup")
})
Picker("\(selectedFilterStatus.title)", selection: $selectedFilterStatus) {
Text(ActiveStatus.active.title).tag(ActiveStatus.active)
Text(ActiveStatus.inactive.title).tag(ActiveStatus.inactive)
}
}
.fullScreenCover(isPresented: $showDetail, content: {
MyDetailsView(presenting: $showDetail)
})
}
.navigationTitle("Main")
}
}
struct MyDetailsView: View {
@Binding var presenting: Bool
var body: some View {
VStack {
Text("Hello from details!")
Button(action: {
presenting.toggle()
}, label: {
HStack {
Image(systemName: "chevron.left")
Text("Back")
}
})
}
}
}
enum ActiveStatus: String, CaseIterable, Identifiable {
case active
case inactive
var id: String { self.rawValue }
}
extension ActiveStatus {
var title: String {
switch self {
case .active:
return "Active for sale"
case .inactive:
return "Inactive"
}
}
}
我完全同意系统中存在错误。但是,您可以绕过它。
这是对我有用的解决方法,已在 ios-15 和 macCatalyst (macos12.01) 设备上测试:
import SwiftUI
@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
@State var selectedFilterStatus = ActiveStatus.active
@State var showDetail: ActiveStatus? // <-- here
var body: some View {
NavigationView {
VStack {
Button(action: {
showDetail = ActiveStatus.active // <-- here
}, label: { Text("Detail popup") })
Picker("\(selectedFilterStatus.title)", selection: $selectedFilterStatus) {
Text(ActiveStatus.active.title).tag(ActiveStatus.active)
Text(ActiveStatus.inactive.title).tag(ActiveStatus.inactive)
}.pickerStyle(.menu)
}
// -- here --
.fullScreenCover(item: $showDetail) { _ in
MyDetailsView()
}
}
.navigationViewStyle(.stack)
.navigationTitle("Main")
}
}
struct MyDetailsView: View {
@Environment(\.dismiss) var dismiss // <-- here
var body: some View {
VStack {
Text("Hello from details!")
Button(action: {
dismiss() // <-- here
}, label: {
HStack {
Image(systemName: "chevron.left")
Text("Back")
}
})
}
}
}
enum ActiveStatus: String, CaseIterable, Identifiable {
case active
case inactive
var id: String { self.rawValue }
}
extension ActiveStatus {
var title: String {
switch self {
case .active:
return "Active for sale"
case .inactive:
return "Inactive"
}
}
}
我有一个选择器在显示和关闭 fullScreenCover 或 sheet 之前工作正常。有谁知道这个示例代码有什么问题,或者有解决方法吗?
我也尝试使用 self.presentation.wrappedValue.dismiss() 关闭 sheet,但结果相同。
示例 gif:https://i.stack.imgur.com/zmcmv.gif
代码:
import SwiftUI
struct ContentView: View {
@State var selectedFilterStatus = ActiveStatus.active
@State var showDetail = false
var body: some View {
NavigationView {
VStack {
Button(action: {
showDetail.toggle()
}, label: {
Text("Detail popup")
})
Picker("\(selectedFilterStatus.title)", selection: $selectedFilterStatus) {
Text(ActiveStatus.active.title).tag(ActiveStatus.active)
Text(ActiveStatus.inactive.title).tag(ActiveStatus.inactive)
}
}
.fullScreenCover(isPresented: $showDetail, content: {
MyDetailsView(presenting: $showDetail)
})
}
.navigationTitle("Main")
}
}
struct MyDetailsView: View {
@Binding var presenting: Bool
var body: some View {
VStack {
Text("Hello from details!")
Button(action: {
presenting.toggle()
}, label: {
HStack {
Image(systemName: "chevron.left")
Text("Back")
}
})
}
}
}
enum ActiveStatus: String, CaseIterable, Identifiable {
case active
case inactive
var id: String { self.rawValue }
}
extension ActiveStatus {
var title: String {
switch self {
case .active:
return "Active for sale"
case .inactive:
return "Inactive"
}
}
}
我完全同意系统中存在错误。但是,您可以绕过它。 这是对我有用的解决方法,已在 ios-15 和 macCatalyst (macos12.01) 设备上测试:
import SwiftUI
@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
@State var selectedFilterStatus = ActiveStatus.active
@State var showDetail: ActiveStatus? // <-- here
var body: some View {
NavigationView {
VStack {
Button(action: {
showDetail = ActiveStatus.active // <-- here
}, label: { Text("Detail popup") })
Picker("\(selectedFilterStatus.title)", selection: $selectedFilterStatus) {
Text(ActiveStatus.active.title).tag(ActiveStatus.active)
Text(ActiveStatus.inactive.title).tag(ActiveStatus.inactive)
}.pickerStyle(.menu)
}
// -- here --
.fullScreenCover(item: $showDetail) { _ in
MyDetailsView()
}
}
.navigationViewStyle(.stack)
.navigationTitle("Main")
}
}
struct MyDetailsView: View {
@Environment(\.dismiss) var dismiss // <-- here
var body: some View {
VStack {
Text("Hello from details!")
Button(action: {
dismiss() // <-- here
}, label: {
HStack {
Image(systemName: "chevron.left")
Text("Back")
}
})
}
}
}
enum ActiveStatus: String, CaseIterable, Identifiable {
case active
case inactive
var id: String { self.rawValue }
}
extension ActiveStatus {
var title: String {
switch self {
case .active:
return "Active for sale"
case .inactive:
return "Inactive"
}
}
}