简体   繁体   English

带有 ForEach 的工具栏,在许多视图更改时消失

[英]Toolbar with ForEach, disappears when many view changes

I have a view with a toolbar.我有一个带有工具栏的视图。 When the view changes (adding an element) the toolbar disappears.当视图更改(添加元素)时,工具栏消失。 If the view is scrolled, the toolbar comes back.如果滚动视图,工具栏会返回。 The example below has a NavigationStack, ScrollView, and VStack, however the problem occurs with just a VStack.下面的示例有一个 NavigationStack、ScrollView 和 VStack,但是只有一个 VStack 会出现问题。

If the ForEach is replaced with discrete Buttons, all works fine.如果将 ForEach 替换为离散按钮,则一切正常。

//
// when the button is presed a Rectangle is shown, and the toolbar disapears
// If the view is scrolled, the toolbar comes back
// If the ForEach in the toolbar is commented out, the problam goes away.
//
// If the button is pressed and held (hilighted) the toolbar is shown until it is released
//
// Commenting out the NavigationStack, the ScrollView or both does not fix the problem.

import SwiftUI

struct ContentView: View {
    @State var showRect = false
    
    var theTools = [myTool(theTool:"highlighter"), myTool(theTool:"pencil")]
    
    var body: some View {
        NavigationStack {
            ScrollView {
                VStack {
                    Spacer()
                    Button("click me") { showRect.toggle() }

                    if (showRect){
                        Rectangle()
                            .fill(Color.red)
                            .frame(width:100, height:100)
                    }
                }
            }
            .frame(height: 500)
        }
        .toolbar {
            ToolbarItemGroup(placement: .bottomBar) {
                Spacer()
                // this code fails, comment out the ForEach block and it works
                ForEach(theTools) {aTool in
                    Button(action: {}) {
                        Image(systemName:aTool.theTool)
                            .resizable()
                            .frame(width: 30, height: 30)
                    }
                }
                // this code works
                Button(action: {}) {
                    Image(systemName:"hand.thumbsup.fill")
                        .resizable()
                        .frame(width: 30, height: 30)
                }
                Spacer()
            }
        }
    }
}

struct myTool: Identifiable
{
    let id:UUID = UUID()
    let theTool:String
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

I expected the toolbar to remain visible.我希望工具栏保持可见。 My code has a lot more stuff in the VStack, but I was able to simplify down to this example.我的代码在 VStack 中有更多的东西,但我能够简化到这个例子。

I'm thinking this is either a "not supported" or a bug in SwiftUI.我认为这要么是“不受支持”,要么是 SwiftUI 中的错误。

I can hard code my tools, bit that seems like the wrong way to go.我可以对我的工具进行硬编码,但这似乎是错误的方法。

Just move the toolbar to the ScrollView instead of the NavigationStack只需将toolbar移动到ScrollView而不是NavigationStack

import SwiftUI

@available(iOS 16.0, *)
struct LoopToolbatView: View {
    @State var showRect = false
    
    var theTools = [myTool(theTool:"highlighter"), myTool(theTool:"pencil")]
    
    var body: some View {
        NavigationStack {
            ScrollView {
                VStack {
                    Spacer()
                    Button("click me") { showRect.toggle() }
                    
                    if (showRect){
                        Rectangle()
                            .fill(Color.red)
                            .frame(width:100, height:100)
                    }
                }
            }
            .frame(height: 500)
            //HERE
            .toolbar {
                ToolbarItemGroup(placement: .bottomBar) {
                    Spacer()
                    // this code fails, comment out the ForEach block and it works
                    ForEach(theTools) {aTool in
                        Button(action: {}) {
                            Image(systemName:aTool.theTool)
                                .resizable()
                                .frame(width: 30, height: 30)
                        }
                    }
                    // this code works
                    Button(action: {}) {
                        Image(systemName:"hand.thumbsup.fill")
                            .resizable()
                            .frame(width: 30, height: 30)
                    }
                    //Spacer()
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM