UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Space at the top of a NavigationLink view

Forums > SwiftUI

I have a layout composed of a ScrollView, inside which there are a couple of VStacks and a Button. It rather nicely fills the screen of an iPhone 8 if it's the main view of the app.

But if I get to it via a NavigationLink, not only is there a "back" button (which I can manage), there's a large area of blank space at the top, above the first item in the ScrollView, pushing the rest of the layout downwards (and the bottom of it off the screen). If I push upwards, the layout scrolls, as it should, but then drops down again when I release it.

I've tried putting a Spacer at the bottom, but it makes no difference.

Any idea what the cause might be?

Jeremy

PS: is there a way to change the text of the NavigationLink's "back" button, to something other than the parent's navigationTitle?

2      

Hi Jeremy,

The Space at the top is for a .navagationTitle("Title"). As this inherits from the first View. You could put the .navigationBarTitleDisplayMode(.inline) and that will put the Title up in the NavBar.

The only way I could think in SwiftUI to change the "back" is to .navigationBarBackButtonHidden(true) and then use a toolbar to add a button of your choice with the dimiss the view code in.

Sample code:

struct DestinationView: View {
    @Environment(\.presentationMode) var presentationMode

    var body: some View {
        ScrollView {
            VStack {
                Text("Hello World!")
            }
        }
        .navigationTitle("Title") // delete if you want no title
        .navigationBarTitleDisplayMode(.inline)
        .navigationBarBackButtonHidden(true) // hides the "back" or previous view title button
        .toolbar {
            ToolbarItem(placement: .navigationBarLeading) {
                Button("Return to sender") {
                    presentationMode.wrappedValue.dismiss() // this changes in iOS15
                }
            }
        }
    }
}

If you want to see what it looks like in the preview then wrap a NavaigationView the view

struct DestinationView_Previews: PreviewProvider {
    static var previews: some View {
        NavigationView {
            DestinationView()
        }
    }
}

PS If someone give you answer that works for you. Then please ☑️ the answer below so that SOLVED shows up. 👇

2      

Thanks, Nigel. You are, of course, perfectly correct.

And I note your point about "solved".

Jeremy

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.