LazyVGrid 内的 NavigationLink 在 swiftUI macOS 中显示不正确

NavigationLink inside LazyVGrid is not displayed correctly in swiftUI macOS

我正在 SwifUI 中开发跨平台应用程序。在 iPhone / iPad 上,此代码在 MacOS 上运行得非常好,而不是当我插入 NavigationLink 时,ForecastCardView 被完全切断。当我删除 NavigationLink 时,一切都正确呈现。

有 NavigationLink

var FullSection: some View {
    LazyVGrid(columns: homeConfigurationUI.columns, alignment: .center, spacing: 20) {
            NavigationLink(destination: Text("test")) {
                  ForecastCardView(viewModel:  ForecastCardViewModel.initForTest())
            }.frame(width: 300, height: 300, alignment: .center)
        }
        .border(Color.yellow)
        .frame(minWidth: 300, idealWidth: 400, maxWidth: 600, minHeight: 0, idealHeight: 500, maxHeight: .infinity, alignment: .center)
}

Image With NavigationLink

没有 NavigationLink

var FullSection: some View {
        LazyVGrid(columns: homeConfigurationUI.columns, alignment: .center, spacing: 20) {
                ForecastCardView(viewModel:  ForecastCardViewModel.initForTest())
            }
            .border(Color.yellow)
            .frame(minWidth: 300, idealWidth: 400, maxWidth: 600, minHeight: 0, idealHeight: 500, maxHeight: .infinity, alignment: .center)
    }

Image Without NavigationLink

一切都在一个ScrollView里面,我尝试插入一个List一个VStack,但是没有结果。我试图在每个组件上放置一个静态框架,但无济于事。

您必须将 buttonStyle 设置为 PlainButtonStyle():

var FullSection: some View {
    LazyVGrid(columns: homeConfigurationUI.columns, alignment: .center, spacing: 20) {
            NavigationLink(destination: Text("test")) {
                  ForecastCardView(viewModel:  ForecastCardViewModel.initForTest())
            }
               .buttonStyle(PlainButtonStyle()) // <— HERE
               .frame(width: 300, height: 300, alignment: .center)
        }
        .border(Color.yellow)
        .frame(minWidth: 300, idealWidth: 400, maxWidth: 600, minHeight: 0, idealHeight: 500, maxHeight: .infinity, alignment: .center)
}