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