NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

Navigation Bar Items

Forums > SwiftUI

I have a view that has a segmented control on it that shows 3 other views

struct HymnsView: View {
    init() {
        UINavigationBar.appearance().backgroundColor = UIColor(named: "ChurchGreen")

        UISegmentedControl.appearance().selectedSegmentTintColor = UIColor(named: "ChurchGreen")
        UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.white], for: .selected)
        UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor(named: "ChurchGreen")!], for: .normal)
    }

    @State private var navBarItems: [String] = ["123", "ABC", "Topics"]
    @State private var navBarChoice = 0

    var body: some View {
        NavigationView {
            VStack {
                Picker(selection: $navBarChoice, label: Text("Select An Option")) {
                    ForEach(0..<self.navBarItems.count) { item in
                        Text(self.navBarItems[item])
                    }
                }.pickerStyle(SegmentedPickerStyle()).padding(10)

                if self.navBarChoice == 0 {
                    HymnsNumericView()
                } else if self.navBarChoice == 1 {
                    HymnsAlphabetView()
                } else if self.navBarChoice == 2 {
                    TopicsView()
                }
            }
            .background(Color("NavBarGreen"))
        } // End of NavigationView
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

Here is the HymnsNumericView which is one of the views the segmented control will show and I've put a navigation bar item button on it.

    var body: some View {
        VStack {
            SearchBar(text: self.$searchTerm, isEditing: self.$isSearching, placeholderText: "by name")

            if self.isSearching {
                List {
                    ForEach(self.sortedHymnList.filter( {
                        self.searchTerm.isEmpty ? true : $0.songName.contains(self.searchTerm)
                    }), id: \.id) { hymn in
                        HymnRowView(hymnData: hymn)
                    }
                }
                .navigationBarTitle("Hymns", displayMode: .inline)
            } else {
                List {
                    ForEach(0..<self.sectionedData.count, id: \.self) { sectionIdx in
                        Section(header: SectionHeaderView(headerName: categories[sectionIdx])) {
                            ForEach(self.sectionedData[sectionIdx], id: \.id) { hymn in
                                HymnRowView(hymnData: hymn)
                            }
                        }
                    }
                }
                .navigationBarTitle("Hymns", displayMode: .inline)
            }
        }
        .navigationBarItems(trailing:
            Button(action: {
                self.showText.toggle()
            }, label: {
                Image(systemName: "plus.square.fill")
                    .font(.largeTitle)
            }).foregroundColor(Color("ChurchGreen"))
        )
        .sheet(isPresented: $showText) {
            // Place code here for new view
            Text("Place Holder")
        }
     }

I am getting the navigation bar item showing up on each view that is presented when you click on a different segment in the segmented control.

Why does the button show up on the HymnsAlphaView and TopicsView and how can I stop it from showing up on the other views?

I have tested this and the button doesn't work on the other views but it still shows up.

Any help would be greatly appreciated.

Thanks Mark

   

I am still looking for an anwser as to why the bar button item would show up on the other views even though i hadnt added it. If someone could explain that to me that would be great.

I did find a work around for this, I just created a blank button that does nothing. Im sure its not the most elegant solution but it works. If someone has a better solution I would love to hear it and understand what I did wrong if I did anything wrong.

   

I think it is because of .navigationViewStyle(StackNavigationViewStyle())

   

Hacking with Swift is sponsored by Instabug

SPONSORED Catch bugs as soon as they happen and know exactly why a crash occurred. Instabug's SDK grabs all the logs they need to fix bugs, crashes and performance issues in minutes instead of days. Get screenshots, device details, network logs, repro steps, and tons of other critical insights needed to resolve issues and prioritize product backlogs straight from your dashboard. It only takes a minute to integrate!

Get started now

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.