SwiftUI 表在 macOS Big Sur 上没有动画解雇

Luk*_*ger 11 swift swiftui macos-big-sur

我希望表格解雇就像外观一样动画但相反。我认为这也是标准行为。例如,当您创建新文件时,您可以在 Xcode 中看到它。

但正如你所看到的,它在没有动画的情况下消失了

演示

这是我的代码:

struct ContentView: View {

    @State var isAnotherViewPresented: Bool = false

    var body: some View {
        HStack {
            Button(action: {
                isAnotherViewPresented.toggle()
            }, label: {
                Text("Button")
            }).sheet(isPresented: $isAnotherViewPresented, content: {
                AnotherView()
            })
        }
        .frame(width: 500, height: 300, alignment: .center)
    }
}

struct AnotherView: View {

    @Environment(\.presentationMode) var presentationMode

    var body: some View {
        VStack {
            Button(action: {
                presentationMode.wrappedValue.dismiss()
            }, label: {
                Text("Close")
            })
        }.padding()
    }
}
Run Code Online (Sandbox Code Playgroud)

我上线了

  • Mac mini (M1, 2020)
  • macOS Big Sur 11.1 (20C69)
  • Xcode 12.3 (12C33)

但我可以在一个

  • Mac mini (2018)
  • macOS Big Sur 11.0.1 (20B29)
  • Xcode 12.2 (12B45b)

arj*_*ndr 4

我终于想出了如何做到这一点,在我的 SwiftUI 应用程序中,如果我在关闭工作表时执行此操作,它就会起作用:

isSheetVisible = false
NSApp.mainWindow?.endSheet(NSApp.keyWindow!)
Run Code Online (Sandbox Code Playgroud)

例子:

struct SheetView: View {
    @Binding var isSheetVisible: Bool

    var body: some View {
        Button("Close") {
            isSheetVisible = false
            NSApp.mainWindow?.endSheet(NSApp.keyWindow!)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在职的

  • 我讨厌这行得通,但我确实行得通。 (3认同)