简体   繁体   English

SwiftUI iOS 的 NavigationView 与 NavigationStack 15/16

[英]SwiftUI NavigationView vs NavigationStack for iOS 15/16

I'm trying to make my iPhone apps (targeting iOS 15 and above) fully compatible with iOS 16 without success!我正在尝试使我的 iPhone 应用程序(针对 iOS 15 及更高版本)与 iOS 16 完全兼容,但没有成功!

I don't know how to have NavigationView for iOS 15 and NavigationStack for iOS 16 in the same piece of code.我不知道如何在同一段代码中拥有 iOS 15 的NavigationView和 iOS 16 的NavigationStack

The code above isn't accepted by Xcode: Xcode 不接受上述代码:

if #available(iOS 16, *) {
   NavigationStack {
} else {
   NavigationView {
}

From: SwiftUI NavigationView/Stack if available iOS 15/16来自: SwiftUI NavigationView/Stack(如果可用)iOS 15/16

I guess that creating a custom container, as suggested by @timmy, will be the only solution.我想按照@timmy 的建议创建自定义容器将是唯一的解决方案。 But I don't know how to proceed.但我不知道如何进行。

What's the solution then?那有什么办法呢?

Personally I wouldn't use NavigationStack unless I would target iOS 16+ but if you want to do that you could make your own Navigation Wrapper我个人不会使用NavigationStack ,除非我的目标是 iOS 16+,但如果你想这样做,你可以制作自己的 Navigation Wrapper

struct MyNavigation<Content>: View where Content: View {
    @ViewBuilder var content: () -> Content
    
    var body: some View {
        if #available(iOS 16, *) {
            NavigationStack(root: content)
        } else {
            NavigationView(content: content)
        }
    }
}

and then just use it, like you would NavigationStack or NavigationView and on iOS 15 or older it would use NavigationView and on iOS 16 or newer it would use NavigationStack然后像使用NavigationStackNavigationView一样使用它,在 iOS 15 或更早版本上,它将使用NavigationView ,在 iOS 16 或更高版本上,它将使用NavigationStack

Your code isn't accepted by Xcode because it isn't valid. Xcode 不接受您的代码,因为它无效。 You cannot have a { without a } in the same block.在同一个块中不能有没有}{

I have found that using NavigationView can present problems on both iPhone and iPad apps running under iOS 16, even though NavigationView is only deprecated for now.我发现使用NavigationView会在 iPhone 和运行在 iOS 16 下的 iPad 应用程序上出现问题,即使NavigationView只是暂时弃用。 On an iPhone, views reached from a NavigationLink often close themselves as soon as they are opened.在 iPhone 上,从NavigationLink的视图通常会在打开后立即自行关闭。 On an iPad, the same problem occurs and the generation of Back arrows appears to be a bit random, especially in document apps.在 iPad 上,出现同样的问题,后退箭头的生成似乎有点随机,尤其是在文档应用程序中。 I have found it well worth making the effort to use NavigationSplitView and NavigationStack , even though this has involved me writing quite a lot of extra code to achieve pleasing results, particularly in apps designed to run at their best on both iPhone and iPad. That said, Apple do provide some clear advice on how to adopt the new Views here .我发现使用NavigationSplitViewNavigationStack是非常值得的,尽管这需要我编写大量额外的代码来获得令人满意的结果,尤其是在设计为在 iPhone 和 iPad 上以最佳状态运行的应用程序中。也就是说, Apple 确实在此处提供了一些关于如何采用新视图的明确建议。

I have come across another oddity with iOS 16. Pickers in modal sheets, which have their list arrays populated .onAppear , no longer work as intended and the Picker selection can no longer be set programmatically.我遇到了另一个奇怪的问题 iOS 16. 模态表中的选择器,其列表 arrays 填充.onAppear ,不再按预期工作,并且选择器选择不能再以编程方式设置。 You have to populate the Picker's list before activating the modal sheet and pass it to the Sheet as a Binding .在激活模式表并将其作为Binding传递给 Sheet 之前,您必须填充 Picker 的列表。

Thanks halo for a top tip on how to use if #available() .感谢halo提供有关如何使用if #available()的重要提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM