SwiftUI 表在第一次出现时被取消 [英] SwiftUI sheet gets dismissed the first time it is presented

查看:33
本文介绍了SwiftUI 表在第一次出现时被取消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个错误让我发疯.有时(大多数情况下)显示的工作表在打开时第一次被关闭.这仅在设备上发生,并且仅在第一次启动应用程序时发生.以下是它在运行 iOS 14.1 的 iPhone 11 上的外观,使用 Xcode 12.1 构建(也可以在运行 iOS 14.0.1 的 iPhone 7 Plus 上重现):

This bug is driving me insane. Sometimes (well most of the time) presented sheet gets dismissed first time it is opened. This is happening only on a device and only the first time the app is launched. Here is how it looks on iPhone 11 running iOS 14.1 built using Xcode 12.1 (can be reproduced on iPhone 7 Plus running iOS 14.0.1 as well):

视频中的步骤:

  1. 我打开应用
  2. 快速导航到详细信息视图
  3. 打开工作表
  4. Red Sheed 被系统解雇了???
  5. 我再次打开工作表,它仍按预期显示在屏幕上.

这是SwitUI App项目(使用UIKIt App Delegate)并部署iOS 14.代码:

This is SwitUI App project (using UIKIt App Delegate) and deployment iOS 14. Code:

struct ContentView: View {
   
    var body: some View { 
        NavigationView {
            NavigationLink(destination: DetailsView()) {
                Text("Open Details View")
            }
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

struct DetailsView: View {
   
    @State var sheetIsPresented: Bool = false
 
    var body: some View {
        VStack {
            Button("Open") {
                sheetIsPresented.toggle()
            }
        }.sheet(isPresented: $sheetIsPresented, content: {
            SheetView()
        })
    }
}

struct SheetView: View {
   
    var body: some View {
        Color.red
    }
}

我可以通过删除 .navigationViewStyle(StackNavigationViewStyle()) 行来解决这个问题,但我的项目中需要 StackNavigationViewStyle.任何帮助将不胜感激.

I was able to fix this problem by removing line .navigationViewStyle(StackNavigationViewStyle()), but I need StackNavigationViewStyle in my project. Any help will be appreciated.

更新:有一个类似的帖子带有工作表视图的 Apple 论坛随机行为很奇怪.我发现的一种解决方案是将工作表移动到 NavigationLink 之外的根视图(在我的示例中为 ContentView),但这不是理想的解决方案.

Updated: There is a similar post on Apple forum with sheet view acting randomly weird. One solution that I found is to move sheet to the root view outside NavigationLink (that would be ContentView in my example), but that is not ideal solution.

推荐答案

我在一个应用中遇到了同样的问题.经过大量研究,我发现将变量设置为观察对象在 SwiftUI 1 中解决了该问题,而在 SwiftUI 2 中似乎也是如此.我记得这是实际设备上的间歇性问题,但它总是发生在模拟器中.我希望我能记住为什么,也许当工作表出现时它会重置绑定变量?但是这段代码解决了这个问题:

I had the same problem in an app. After a great deal of research, I found that making the variable an observed object fixed the problem in SwiftUI 1, and it seems to be in SwiftUI 2. I do remember that it was an intermittent problem on an actual device, but it always happened in the simulator. I wish I could remember why, maybe when the sheet appears it resets the bound variable?, but this code fixes the problem:

import SwiftUI
import Combine

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: DetailsView()) {
                Text("Open Details View")
            }
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

struct DetailsView: View {
   
    @ObservedObject var sheetIsPresented = SheetIsPresented.shared
    
    var body: some View {
        VStack {
            Button("Open") {
                sheetIsPresented.value.toggle()
            }
        }.sheet(isPresented: $sheetIsPresented.value, content: {
            SheetView()
        })
    }
}

struct SheetView: View {
   
    var body: some View {
        Color.red
    }
}

final class SheetIsPresented: NSObject, ObservableObject {
    let objectWillChange = PassthroughSubject<Void, Never>()
    
    static let shared = SheetIsPresented()
    
    @Published var value: Bool = false {
        willSet {
            objectWillChange.send()
        }
    }
    
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

在模拟器中在 Xcode 12.1、iOS 14.1 上测试.

Tested on Xcode 12.1, iOS 14.1 in simulator.

这篇关于SwiftUI 表在第一次出现时被取消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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