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

SOLVED: In SwiftUI, a NavigationBarItem, in a View pushed by a NavigationLink embedded inside a ScrollView inside a List or Form, doesn't work

Forums > SwiftUI

When using a NavigationLink inside a ScrollView inside a List or Form to push TestDetail(), the NavigationLink works, but the navigationBarItems in TestDetail() do not work. In fact, the navigationBarItems aren't recognised as buttons.

Here is a code snippet which replicates the issue. Notice that item 1 works, but item 2 fails. Can anyone please advise me on what I might be doing wrong?

struct TestMaster: View {
    var body: some View {
        NavigationView {
            List {
                /// 1. Success - NavigationLink pushes TestDetail and resulting NavigationBarItem works
                NavigationLink(destination: TestDetail()) {
                    Text("1. Resulting NavigationBarItem in pushed TestDetail() works - Success")
                }

                /// 2. Failure - NavigationLink inside ScrollView pushes TestDetail, but NavigationBarItem doesn't work
                ScrollView(.horizontal, showsIndicators: false) {
                    HStack {
                        NavigationLink(destination: TestDetail()) {
                            Text("2. Resulting NavigationBarItem in pushed TestDetail() does not work - Failure")
                        }
                    }
                }
            }
        }
    }
}

struct TestDetail: View {
    var body: some View {
        Text("Not really a view, just testing above NavigationBarItems ...")
            .navigationBarItems(trailing:
                Button(action: {
                    print("printing something ...")
                }, label: {
                    Text("Print")
                })
        )
    }
}

3      

This issue has been resolved in SwiftUI "2.0" using Xcode 12.0 beta 2 (12A6163b)

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.