带有嵌套条件行为的奇怪工具栏 [英] Weird toolbar with nested conditionals behavior

查看:22
本文介绍了带有嵌套条件行为的奇怪工具栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个非常奇怪的问题,如果您有一个很深的 ViewBuilder 和 if 语句链以及工具栏,工具栏中的某些项目会重复.

I'm getting this extremely odd issue where if you have a deep chain of ViewBuilder's and if statements, along with a toolbar, some of the items in the toolbar duplicate.

这是一张图片来表达我的意思(注意 2 个按我!"按钮)

Here is an image to show what I mean (Notice the 2 "Press me!" buttons)

复制:(注意:我使用的是 macOS Monterey Beta 5 和 Xcode 13 Beta 5)

  1. 创建一个新的空白 SwiftUI macOS 项目
  2. 将此代码粘贴到 ContentView 中:

struct ContentView: View {
    var body: some View {
        NavigationView {
            Text("Sidebar")
            SecondPanel()
        }
    }
}
struct SecondPanel: View {
    @State var num = 0
    
    @ViewBuilder var content: some View {
        Text("Line 2")
        Text("Line 3")
    }
    @ViewBuilder var contentWithToolbar: some View {
        content
            .toolbar {
                Button(action: {
                    num += 1
                }) {
                    Text("Press me!")
                }
            }
    }
    var body: some View {
        if num == 0 {
            contentWithToolbar
                .navigationSubtitle("Num is zero!")
        }
        else {
            contentWithToolbar
        }
    }
}

  1. 运行应用.你应该看到两个按我!"按钮,尽管我只有一个工具栏按钮!

我想知道这是怎么回事,有什么办法可以解决这个问题吗?

I was wondering, what is going here, and is there any way I can solve this problem?

推荐答案

@Jake 指出问题是由 2 个 Text 对象在没有 VStack 的情况下堆叠引起的.

@Jake pointed out that the problem was caused by the 2 Text objects being stacked without a VStack.

添加一个 VStack 完全解决了这个问题.

Adding a VStack fixed the problem completely.

这篇关于带有嵌套条件行为的奇怪工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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