WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Large gap in UI

Forums > SwiftUI

Hi, I have searched a number of topics on this and the suggested solutions do not seem to work. Hoping someone here can help.

In the UI there is a large gap above the "Banner" image at the top. I'm assuming this is the space for the Navigation Bar Title but have hidden Navigation Bar.

Here is the code:

struct WelcomeView: View {
    @State private var angle = 0.0

    var body: some View {
        NavigationView {
            VStack {
                GeometryReader { geo in
                    Image("Banner")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: geo.size.width * 1.0)
                        .frame(width: geo.size.width, height: geo.size.height)
                }

                ZStack {

                    Button (action: {
                        self.angle += Double.random(in: 3600..<3960)
                    }) {
                        GeometryReader { geo in
                            Image("wheel")
                                .resizable()
                                .scaledToFill()
                                .frame(width: geo.size.width * 0.6)
                                .frame(width: geo.size.width, height: geo.size.height)
                                .rotationEffect(.degrees(self.angle))
                                .animation(.timingCurve(0, 0.8, 0.2, 1, duration: 10), value: angle)
                                .shadow(radius:15)
                        }
                    }

                    Circle()
                        .fill(Color.white)
                        .frame(height:50)
                        .shadow(radius: 5)

                    Image(systemName: "arrowtriangle.up.fill")
                        .font(.system(size: 30))
                        .foregroundColor(.white)
                        .padding(.bottom, 40)
                }

                Text("Take the quiz and find out which colour best matches your FPL style")
                    .font(.title2)
                    .padding()

                NavigationLink(destination: QuizView(), label: {
                   Text ("Start Quiz")
                        .font(.title)
                        .padding()
                })
            }
        }
        .navigationBarHidden(true)
    }
}

Please does anyone have any suggestions on how to move the image to the top of the screen? Thanks in advance

   

Is this close to what you are after?

let yOffset: CGFloat = 50
…

Image("Banner")
    .resizable()
    .aspectRatio(contentMode: .fit)
    .frame(width: geo.size.width * 0.8)
    .frame(width: geo.size.width, height: geo.size.height)
}
.offset(X: 0, y: yOffset)
.edgesIgnoringSafeArea([.top])

   

Thanks @Greenamberred, aplying that offset to the banner image helps me to pull it to the top of the screen.

I'm not sure if this was what I was looking for in a solution though and I am new to this so please excuse if I worded incorrectly.

I essentially have 4 elements to this welcome view;

  • Banner image (top)
  • Spinning wheel image (about half the screen but aligned just north of centre)
  • Some text (equally spaced between wheel and start quiz)
  • Start Quiz button (bottom)

My intention is to control the layout of these using frames, padding and spacers. But the UI is squishing them down with this white space at the top.

Your solution pulls the banner image into the white space using the offset. However, the remaining items remained squished towards the bottom of the UI.

My thinking is I could use other yOffset values for these elements too but then I am artificially pulling them up or down the UI and not sure this would scale well between devices. I could be wrong though.

Does that make sense? As I said, I am still learning and what I am aspiring to won't work but if I had the same elements laid out in a non Navigation View, they would layout evenly without this white space no?

   

I'm only on mobile but maybe VStack(spacing: 0)? Or edgesIgnoringSafeArea(.top)?

   

I have found the solution and it was much more simpler than expected.

Went back to watch one of Paul's videos on Navigation views and found that my navigationBarHidden modifier was at the wrong level, moving it to modifier what is in the Navigation View resolved the issue

   

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.