我有一个导航链接:

NavigationLink("Login") {
                        WelcomeView()
                    }.foregroundColor(.white).frame(width:300, height: 50).background(Color.blue).cornerRadius(10).padding().disabled(authenticateUser())

它看起来像这样:

enter image description here

问题是,只有当用户直接点击单词"Login"时,导航才会发生.如果用户点击文本之外的蓝色区域,则不会出现导航,也不会发生任何事情.

如何修改NavigationLink,以便用户可以点击其框架内的任何位置以导航到链接的In视图?

推荐答案

目前,您正在设置NavigationLink的背景样式,但只有文本可点击.相反,要利用NavigationLink.init(destination: (), label: ().下面是一个使用您所做的操作的示例,但允许单击整个按钮:

NavigationLink {
                //Your destination here
                WelcomeView()
            } label: {
                ZStack{
                    RoundedRectangle(cornerRadius: 10)
                        .foregroundColor(.blue)
                    Text("Login")
                        .foregroundColor(.white)
                }
            }.frame(width: 300, height: 50)
            .padding().disabled(authenticateUser())

Also, somewhat off topic but I might have the disabled be based on the result of a ViewModel that you call at the start in an @Published variable - using .onAppear and checking then. That would make it so you could update it if necessary. Here's a video of the behavior now: A GIF of the new behavior

Ios相关问答推荐

缺少预期的键:';NSPrival yCollectedDataTypes';

设置堆栈视图的所有属性

swiftui动画如何实时确定不对称过渡

AVAudioRecord 没有音频

uikit中如何触发swiftui功能

在左侧显示多行值时会出现 SwiftUI 错误,这会 destruct 堆栈中右侧的对齐方式

从 PHAssets 生成图像时,iOS 应用程序因内存问题而崩溃

过渡动画在 iOS16 中不起作用,但在 iOS15 中起作用

使用 Apollo 发布 iOS 应用程序时缺少推送通知权利

如何让替换视图淡入旧视图而不是交叉淡入淡出?

react 在 android 上运行的本机应用程序,但在 iOS 上出现问题,详细信息在描述中共享

SwiftUI 我自己的渲染函数的返回类型是什么?

如何 comments .plist 文件中的行?

iPhone 6 和 6 Plus 媒体查询

如何获取 UICollectionViewCell 的矩形?

通过touch 传递到下面的 UIViews

获取 iOS 上所有联系人的列表

iOS 10 错误:UICollectionView 接收到具有不存在索引路径的单元格的布局属性

Select 器touchesBegan:withEvent:的覆盖方法具有不兼容的类型(NSSet,UIEvent)->()

如何用 CAShapeLayer 和 UIBezierPath 画一个光滑的圆?