SwiftUI 表在 macOS Big Sur 上没有动画解雇 [英] SwiftUI sheet not animating dismissal on macOS Big Sur

查看:43
本文介绍了SwiftUI 表在 macOS Big Sur 上没有动画解雇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like the sheet dismissal animated just like the appearance but reversed. I think this is also the standard behavior. You can see it in Xcode for example when you create a new file.

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

But as you can see it just disappears without animation

这是我的代码:

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

我在

  • 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)

推荐答案

我终于想出了怎么做,在我的 SwiftUI 应用程序中,如果我在关闭工作表时执行此操作:

I finally figured out how to do it, in my SwiftUI app it works if I do this while closing the sheet:

isSheetVisible = false
NSApp.mainWindow?.endSheet(NSApp.keyWindow!)

示例:

struct SheetView: View {
    @Binding var isSheetVisible: Bool

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

这篇关于SwiftUI 表在 macOS Big Sur 上没有动画解雇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆