NavigationLink中的tvOS按钮不起作用

问题描述 投票:0回答:1

我已经使用swift和swiftui构建了一个iOS应用,现在我正在开发该应用的tvOS版本。但是,我有几个问题。问题之一仍未解决:tvOS Textfield Transparent Background我正在创建一个登录页面,但我意识到NavigationLink中的按钮没有执行任何操作。然后,为了测试并确定问题,我创建了一个简单的按钮,如下所示:

Button(action: {
    print("Login pressed")
}) {
    Text("Login")
        .frame(width:300, height: 35)
}
.background(Color.blue)
.foregroundColor(Color.white)

单击时输出:

Login pressed

它看起来像这样:

enter image description here

并且在聚焦时看起来像这样:

enter image description here

但是,当我将该按钮插入到NavigationLink中时,导航就像按钮一样,而此按钮则什么也不做。

代码是这样的:

NavigationLink(destination: mainScene(), tag:1, selection: $selection) {

    Button(action: {
        print("Login pressed")
        self.selection = 1
    }) {
        Text("Login")
            .frame(width:300, height: 35)
    }
    .background(Color.blue)
    .foregroundColor(Color.white)
}

在iOS中,我有一个类似的代码,它不会导航,直到执行了按钮动作中的所有操作并且选择等于1为止。但是在这种情况下,它不会执行任何操作,而只是直接导航到下一页。当我将其嵌入到NavigationLink中时,该按钮如下所示:

enter image description here

您可以看到,原始登录按钮周围有空白,并且焦点位于该空白上。并在单击时进行导航。看起来NavigationLink就像按钮一样,但是它也阻止了按钮动作的执行。我遇到的这类问题主要是由焦点引起的。例如,在上图中的示例中,您可以看到当焦点打开时按钮的形状和颜色发生了变化,但我想对其进行控制。但是,我不知道该如何专注于项目的焦点设计。

swift swiftui tvos
1个回答
0
投票

尝试以下操作

Button(action: {
    print("Login pressed")
    self.selection = 1
}) {
    Text("Login")
        .frame(width:300, height: 35)
}
.background(Color.blue)
.foregroundColor(Color.white)
.background(NavigationLink(destination: mainScene(), tag:1, 
     selection: $selection) { EmptyView() })
© www.soinside.com 2019 - 2024. All rights reserved.