NavigationLink 中的 tvOS 按钮不起作用 [英] tvOS Button inside NavigationLink is not Working

查看:25
本文介绍了NavigationLink 中的 tvOS 按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 swift 和 swiftui 构建了一个 iOS 应用程序,现在我正在开发该应用程序的 tvOS 版本.但是,我有几个问题.问题之一仍未解决:

聚焦时看起来像这样:

但是当我在 NavigationLink 中插入那个按钮时,Navigation 就像一个按钮,而这个按钮什么也不做.

代码是这样的:

NavigationLink(destination: mainScene(), tag:1, selection: $selection) {按钮(动作:{打印(登录被按下")自我选择 = 1}) {文本(登录").frame(宽:300,高:35)}.background(Color.blue).foregroundColor(Color.white)}

在iOS中,我有一个类似的代码,它不会导航,直到按钮动作中的所有内容都被执行并且选择等于1.但在这种情况下,它不执行任何内容,它只是直接导航到下一个页.当我将它嵌入到 NavigationLink 中时,按钮看起来像这样:

如您所见,原始登录按钮周围有空白区域,焦点位于该空白区域.当点击它导航.看起来 NavigationLink 就像一个按钮,但它也阻止了按钮操作的执行.我遇到了这些主要是由焦点引起的问题.例如在上图中,您可以看到当焦点位于按钮上时,按钮的形状和颜色会发生变化,但我想控制它.但是,我不知道如何在项目的焦点设计上发挥作用.

解决方案

试试这个:

NavigationLink(destination: mainScene(), tag:1, selection: $selection) {按钮(动作:{打印(登录被按下")自我选择 = 1}) {文本(登录").frame(宽:300,高:35)}.background(Color.blue).foregroundColor(Color.white)}.buttonStyle(PlainButtonStyle())

I have built an iOS app using swift and swiftui and now I am working on the tvOS version of the app. However, I have a couple of issues. One of the issues is still unresolved: tvOS Textfield Transparent Background I was creating a login page and I realized that the button inside the NavigationLink was not doing anything. Then to test and identify the issue I created a simple button as follows:

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

Output when clicked on:

Login pressed

And it looks like this:

And it looks like this when focused:

But when I insert that button inside a NavigationLink, Navigation like acts like a button and this button does nothing.

The code is like that:

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)
}

In the iOS, I have a similar code, and it does not navigate until everything in the button action is executed and selection is equal to 1. But in this, it does not execute anything and it just directly navigates to the next page. The button looks like this when I embed it in the NavigationLink:

As you can see there is white space around the original login button and the focus is on that white space. And when clicked on it navigates. It looks like the NavigationLink acts like a button but it also prevents the button action to be executed. I am having these kind of problems that are mainly caused by the focus. For Example in the images above, as you can see the shape and the color of the button changes when the focus is on but I want to control it. However, I don't know how I can play with on focus design of the items.

解决方案

Try This:

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)
}.buttonStyle(PlainButtonStyle())

这篇关于NavigationLink 中的 tvOS 按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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