设想:
.sheetSwiftUI 会认为被.sheet解雇了,但实际上,只有 DatePicker 的模式被解雇了。
最小代码示例:
struct DateScreen: View {
@Binding var isPresented: Bool
@State var date: Date = Date()
var body: some View {
NavigationView {
VStack {
DatePicker("", selection: $date, displayedComponents: [.hourAndMinute])
.datePickerStyle(CompactDatePickerStyle())
}
.navigationBarItems(leading: Button("Dismiss") {
isPresented = false
})
}
}
}
@main
struct Main: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@State var isPresenting: Bool = false
var body: some Scene {
WindowGroup {
Button("Present modal", action: {
isPresenting = true
})
.sheet(isPresented: $isPresenting, content: {
DateScreen(isPresented: $isPresenting)
})
}
}
}
Run Code Online (Sandbox Code Playgroud)
显示损坏行为的 GIF:
请注意,如果用户没有打开数字键盘,它似乎工作正常。
这是提供的代码的问题 - 状态State不是Scene视图 - 状态不是为了更新场景而设计的。正确的 SwiftUI 解决方案是将所有内容从场景移动到视图,并且那里只有一个根视图,即。
使用 Xcode 13.4 / iOS 15.5 进行测试
@main
struct Main: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView() // << window root view, the one !!
}
}
}
struct ContentView: View {
@State var isPresenting: Bool = false
var body: some View {
Button("Present modal", action: {
isPresenting = true
})
.sheet(isPresented: $isPresenting, content: {
DateScreen(isPresented: $isPresenting)
})
}
}
// no more changes needed
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1066 次 |
| 最近记录: |