'Image at top of view extending under navigationbar

I am trying to make a "reusable" template for views in my app. As part of this I started prototyping this:

    var body: some View {
        NavigationView {
            ZStack {
                VStack {
//                    Spacer()
                    Image("progress_bar")
                        .resizable()
                        .scaledToFit()
                        .foregroundColor(Color.gray)
                        .background(Color.green)
                    HStack{
                    }
                    Spacer()
                }
                VStack{
                }
            }
        }
        .navigationBarHidden(true)
    }
}

The ZStack contains 2 VStack. First one is my template and will be part of multiple of my screens later on. Second Stack is destined to be replaced by @ViewBuilder parameter so that I can reuse that in multiple screens easily.

The progress_bar image is a SVG file imported into assets, preserving Vector Data and rendered as template (So I can change colour).

My issue, as shown on the following screenshot, is that the image somehow extends toward the top of the screen. The green area correspond to the green coloured background added to the image. The progress bar is the grey line across the screen.

progress bar extending toward top of the screen

If I change my code to (commented out the spacer):

//                    Spacer()
                    Image("progress_bar")
                        .resizable()
                        .scaledToFit()
                        .foregroundColor(Color.gray)
                        .background(Color.green)
                    HStack{
                    }
                    Spacer()
                }

I get this, progress bar shifts down in the screen (not wanted but expected) but the green area that was added on top of the image disappears:

updated screen with progress_bar shifted down and not over extending

I did try setting up a maxHeight to my Image view but this did not work out.

What am I missing? Is there a way I can stop this from happening?

Edit: After more looking around, my issue is coming from the fact that the whole thing is embedded in a NavigationView. Apparently space is saved for the navigation bar even though it is hidden.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source