SwiftUI:macOS应用的全屏覆盖等效物是什么?

3

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

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

这很有效。然而,在macOS上此方法不可用。有没有类似的方法可以在macOS上实现?

1个回答

2

fullScreenCover(isPresented:onDismiss:content:)Mac Catalyst 14.0+ 中得到支持。

启用目标的 Mac 支持:

enter image description here

用于在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()
                    }
                })
            }
        }
    }
}

2
我本来不想选择Catalyst这条路线,因为它会让应用看起来更像是一个iPad应用,而不是一个原生的macOS应用。如果我启用Catalyst只是为了获得这个功能,会有什么后果呢?它会继续像一个macOS应用一样运行吗?还是会默认为iPad的行为方式? - undefined
1
感觉就像一个Mac应用程序,选项“优化Mac界面”会将控件替换为macOS的对应项。如果你想选择多平台路线,会更加复杂,因为在macOS上很多修饰符是不可用的。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接