Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jbadger3/3dc5bf33915c75d8552ecb3e16c9a6d6 to your computer and use it in GitHub Desktop.
Save jbadger3/3dc5bf33915c75d8552ecb3e16c9a6d6 to your computer and use it in GitHub Desktop.
SwiftUI: Overlapping Navigation Titles
struct FoodGroup {
var name: String
var examples: [String]
}
struct ContentView: View {
var foodGroups: [FoodGroup] {
let fruits = FoodGroup(name: "Fruit", examples: ["Apple", "Banana", "Pear", "Peach", "Mango", "Orange", "Strawberry", "Watermelon", "Pineapple", "Lemon", "Lime", "Cherry", "Date", "Plum", "Apricot", "Blueberry", "Blackberry", "Cranberry", "Kiwi", "Nectarine"])
let vegetables = FoodGroup(name: "Vegetables", examples: ["Lettuce", "Carrot", "Beet", "Broccoli", "Corn", "Celery", "Chicory", "Kale", "Spinach", "Yarrow", "Brussels sprouts", "Arugula", "Cauliflower", "Turnip", "Sweet Potato"])
return [fruits, vegetables]
}
@State var isDetailLink = true
var body: some View {
NavigationView {
List {
ForEach(foodGroups, id: \.name) { foodGroup in
NavigationLink(
destination: DetailView(foodGroup: foodGroup),
label: {
Text(foodGroup.name)
})
.isDetailLink(isDetailLink)
}
Button(action: {isDetailLink.toggle()}, label: {
Text("Detail link: \(String(isDetailLink))")
})
.buttonStyle(BorderlessButtonStyle())
}
.navigationTitle(Text("Food Groups"))
}
}
}
struct DetailView: View {
@Environment(\.presentationMode) var presentationMode
var foodGroup: FoodGroup
@State var navDisplayMode: NavigationBarItem.TitleDisplayMode = .large
var body: some View {
Form {
ForEach(foodGroup.examples, id: \.self) { foodName in
Text(foodName)
}
Button(action: {
if navDisplayMode == .large {
navDisplayMode = .inline
} else {
navDisplayMode = .large
}
}, label: {
Text("Nav Display Mode: \(navDisplayMode == .large ? "large" : "inline")")
})
HStack {
Spacer()
Button(action: {dismissView()}, label: {
Text("Save")
})
Spacer()
}
}
.navigationBarTitle(foodGroup.name, displayMode: navDisplayMode)
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: Button(action: {self.dismissView()}, label: {
Text("Cancel")
}))
}
func dismissView() {
self.presentationMode.wrappedValue.dismiss()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment