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

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

2      

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.

2      

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

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.