简体   繁体   English

为什么 EmptyView() 在带有 SwiftUI 的 WatchOS 上不起作用?

[英]Why doesn't EmptyView() work on WatchOS with SwiftUI?

NavigationView() doesn't work on WatchOS under swiftUI but I found a neat trick(?) here to get the NavigationView working on WatchOS. NavigationView()在 swiftUI 下的 WatchOS 上不起作用,但我发现了一个巧妙的技巧(?) 在这里让 NavigationView 在 WatchOS 上工作。 Adding this allows one to use NavigationView() in watchOS projects添加此项允许在 watchOS 项目中使用NavigationView()

#if os(watchOS)
struct NavigationView<Content: View>: View {
    let content: () -> Content

    init(@ViewBuilder content: @escaping () -> Content) {
        self.content = content
    }

    var body: some View {
        VStack(spacing: 0) {
            content()
        }
    }
}
#endif

The problem is, a method I want to use to pop a new view from a function (like a button) requires a NavigationLink which shows an EmptyView() , and while that works on iOS it doesn't work on WatchOS.问题是,我想用来从 function (如按钮)弹出一个新视图的方法需要一个显示EmptyView()的 NavigationLink ,虽然它适用于 iOS ,但它不适用于 WatchOS。

Working code on iOS: iOS 上的工作代码:

import SwiftUI

struct ContentView: View {
    
    @State var showNewView = false
    
    var body: some View {
        NavigationView{
            VStack {
            NavigationLink(destination: Text("This text view"), isActive: self.$showNewView) {EmptyView()}
            
                Button("Not the world!") {
                    self.showNewView = true
                }
            Text("Hello, World!")
            }
        }
    }
}

If you put that code in to a new WatchOS application, and include the relevant code snippet to make NavigationView work on WatchOS, it allows the application to be made, but the button to change Views won't do anything.如果您将该代码放入一个新的 WatchOS 应用程序中,并包含相关代码片段以使 NavigationView 在 WatchOS 上工作,它允许创建应用程序,但更改视图的按钮不会执行任何操作。

It will change the variable showNewView to true (I've tested by having a print statement) but that becoming true won't show the new view.它会将变量showNewView更改为 true (我已经通过print语句进行了测试),但变为 true 不会显示新视图。

If I put a non-emptyview (Something like Text("Merry Christmas") ) in there, it will work.如果我在其中放置一个非空视图(类似于Text("Merry Christmas") ),它将起作用。

I'm currently looking for another solution to my problem of getting a new view to show in SwiftUI through a function (not through making some sort of list with NavigationView), but I am curious about what is going on.我目前正在寻找另一种解决方案来解决我的问题,即通过 function(不是通过使用 NavigationView 制作某种列表)在 SwiftUI 中显示新视图,但我很好奇发生了什么。 I assume it has something to do with hack used to get NavigationView working on WatchOS, but I don't really know how that code snippet is workin.我认为这与用于让 NavigationView 在 WatchOS 上工作的黑客行为有关,但我真的不知道该代码片段是如何工作的。

Here is a solution - just put navigation link into background.这是一个解决方案 - 只需将导航链接放入后台即可。

Tested with Xcode 11.4用 Xcode 11.4 测试

VStack {
    Button("Not the world!") {
        self.showNewView = true
    }
    .background(NavigationLink(destination: Text("This text view"), 
         isActive: self.$showNewView) {EmptyView()} )

    Text("Hello, World!")
}

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

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