ios - SwiftUI:macOS 应用程序的 fullScreenCover 等效项?
问题描述
我正在编写一个跨平台的 SwiftUI 应用程序,如果用户希望“锁定”应用程序,它需要提供密码提示。锁定屏幕应覆盖应用程序中的所有视图,直到用户成功进行身份验证。在 iOS 上,我可以使用以下fullScreenCover
方法执行此操作:
.fullScreenCover(isPresented: $isLocked, content: {
ApplicationLockView(viewModel: ApplicationLockViewModel())
})
这很好用。但是,此方法在 macOS 上不可用。是否有在 macOS 上实现此功能的等效方法?
解决方案
fullScreenCover(isPresented:onDismiss:content:)
确实支持Mac Catalyst 14.0+。
为目标启用 mac 支持:
用于在 mac 上测试的源代码:
struct ContentView: View {
@State private var isPresented = false
var body: some View {
Button("Present") {
isPresented.toggle()
}
.fullScreenCover(isPresented: $isPresented) {
ModalView()
}
}
}
struct ModalView: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
Button("Dismiss") {
presentationMode.wrappedValue.dismiss()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.blue)
.edgesIgnoringSafeArea(.all)
}
}
使用移动边缘过渡:
struct ContentView: View {
@State private var isPresented = false
var body: some View {
ZStack {
Button("Present", action: {
withAnimation(.linear) {
self.isPresented.toggle()
}
})
if isPresented {
ModalView(isPresented: self.$isPresented).transition(.move(edge: .bottom))
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
struct ModalView: View {
@Binding var isPresented: Bool
var body: some View {
ZStack {
Rectangle()
.fill(Color.blue)
.frame(maxWidth: .infinity, maxHeight: .infinity)
VStack {
Button("Dismiss",action: {
withAnimation(.linear) {
self.isPresented.toggle()
}
})
}
}
}
}
推荐阅读
- python - python subtraction with list, without the first one
- rust - 如何从 Stream 特征中读取
- c# - HTTP MVC 错误 | 将 React 与 ASP.Net 集成
- firebase - 尝试为 Android 应用启用 Cloud Vision 文本识别 API
- pine-script - tradingview 占位符未处于警报状态
- apache - Apache 配置文件中的禁用目录列表被忽略
- google-sheets - 在瀑布图中将 x 轴解释为位置,而不是文本
- wpf - wpf应用程序位于最顶层时如何禁用转义键
- cuda - 关于accroutine的一些问题
- django - 如何使用 IP 访问 Vue.js 应用程序