【问题标题】:SwiftUI: fullScreenCover equivalent for macOS apps?SwiftUI:macOS 应用程序的 fullScreenCover 等效项?
【发布时间】:2021-10-19 12:53:09
【问题描述】:

我正在编写一个跨平台的 SwiftUI 应用程序,如果用户希望“锁定”应用程序,它需要提供密码提示。锁定屏幕应覆盖应用程序中的所有视图,直到用户成功进行身份验证。在 iOS 上,我可以使用这样的 fullScreenCover 方法来做到这一点:

.fullScreenCover(isPresented: $isLocked, content: {
        ApplicationLockView(viewModel: ApplicationLockViewModel())
    })

这很好用。但是,此方法在 macOS 上不可用。是否有在 macOS 上实现此功能的等效方法?

【问题讨论】:

    标签: ios swift macos swiftui


    【解决方案1】:

    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()
                        }
                    })
                }
            }
        }
    }
    

    【讨论】:

    • 我不想走 Catalyst 路线,因为它使应用程序的外观和感觉更像 iPad 应用程序,而不是原生 macOS 应用程序。如果我启用 Catalyst 只是为了获得这一功能,会有什么后果?它会继续像 macOS 应用程序一样运行吗?还是会默认为 iPad 行为?
    • 感觉就像一个 Mac 应用程序,因为选项 Optimize Interface for Mac 用 macOS 对应项替换了控件。如果你想走多平台路线,它会更复杂,因为 macOS 上没有这么多修饰符。
    猜你喜欢
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-01
    • 2010-10-25
    • 1970-01-01
    相关资源
    最近更新 更多