简体   繁体   English

tvOS 上的 NavigationLink 按钮与 SwiftUI 不工作

[英]NavigationLink buttons on tvOS with SwiftUI not working

I am trying to build a simple navigation UI on Apple TV with SwiftUI:我正在尝试使用 SwiftUI 在 Apple TV 上构建一个简单的导航 UI:

,

As I can tell, I need to use either NavigationLink or NavigationLink combined with Button .据我所知,我需要将NavigationLinkNavigationLinkButton结合使用。

I have tried several implementations and none of them worked:我已经尝试了几种实现,但都没有奏效:

        NavigationLink(destination: view2) {
            Image("placeholder").frame(width:400, height: 300)
            Text("Button")
        }

        NavigationLink(destination: view2) {
            Button(action: {print("hey")}) {
                VStack{
                    Image("placeholder").frame(width:400, height: 300)
                    Text("Button")
                }
            }
        }

        Button(action: {print("hi1")}) {
            VStack{
                Image("placeholder").frame(width:400, height: 300)
                Text("Button")
            }
        }.background(NavigationLink(destination: view2) { Text("hi2") })



         NavigationLink(destination: view2) {
            Text("hey")
         }.background(Button(action: {print("hey")}) {
            VStack{
                Image("placeholder").frame(width:400, height: 300)
                Text("Button")
            }
        })

The first two ones are not selectable with Magic Remote: they won't become focused.前两个不能用 Magic Remote 选择:它们不会变得专注。 The last ones are simply not navigating to another view when I press on them.当我按下它们时,最后一个根本不会导航到另一个视图。

How do I implement this style of navigation on tvOS with SwiftUI?如何使用 SwiftUI 在 tvOS 上实现这种导航方式?

NavigationLink works by itself, standalone, only on watchOS (that might confuse), in all other supported OSs it should be included in NavigationView to operate, so NavigationLink独立工作,仅在watchOS上(可能会混淆),在所有其他受支持的操作系统中,它应该包含在NavigationView中才能运行,所以

in pseudo-code在伪代码中

  NavigationView {
     // ... some code

     NavigationLink(...)   // must be anywhere inside

     // ... other code
  }

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

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