How to stop showing Detail view when item in Master view deleted?
Try the following (not tested, just idea - on delete there is not new selection/navigation in stack so details view is not refreshed, so we can try to force that refresh explicitly):
struct ContentView: View {
@State private var items = ["Item 1", "Item 2", "Item 3"]
@State private var refreshID = UUID() // << this !!
var body: some View {
NavigationView {
List {
ForEach(items, id: \.self) { item in
NavigationLink(destination: DetailView(item: item)) {
Text(item)
}
}
.onDelete(perform: delete)
}
Text("Please select an item.")
}
.id(refreshID) // << here !!
}
func delete(at offsets: IndexSet) {
items.remove(atOffsets: offsets)
refreshID = UUID() // << here !!
}
}
Three Column Navigation View does not update until you click to show the sidebar on iPad
The problem is simply that the NavigationLink
is not in existence when the .onAppear
is called. .onAppear
sets link1Active
to true (I verified this by printing the value in your print()
statement in .onAppear
), but nothing is using it until the main List
view is opened and on screen. I really don't think this is a true "fix", but the only way you are going to show the destination is simple with a conditional like:
(!link1Active ? Text("View 1") : Text("Link1 Destination"))
Since this is a minimal reproducible example, I am not sure of your use case, but I think you need to rethink how you are showing your views.
Navigation View not working properly in SwiftUI on iPad
As I commented there should be only one NavigationView
, so here fixed ProductDetailView
with removed redundant NavigationView
.
Tested with Xcode 12
struct ProductDetailView: View {
var product: Product
var products: [Product] = productData
@State var showingPreview = false
var body: some View {
ScrollView(.vertical, showsIndicators: false) {
VStack(alignment: .center, spacing: 20) {
ProductHeaderView(product: product)
VStack(alignment: .leading, spacing: 15) {
Text(product.title)
.font(.largeTitle)
.fontWeight(.heavy)
Text(product.headline)
.font(.headline)
.multilineTextAlignment(.leading)
Text("Learn More About \(product.title)".uppercased())
.fontWeight(.bold)
.padding(0)
Text(product.description)
.multilineTextAlignment(.leading)
.padding(.bottom, 10)
}
.padding(.horizontal, 20)
.frame(maxWidth: 640, alignment: .center)
}
.navigationBarTitle(product.title, displayMode: .inline)
.navigationBarHidden(true)
}
.edgesIgnoringSafeArea(.top)
}
}
Unwind NavigationView to root when switching tabs in SwiftUI
You'll need to keep track of the tab selection in the parent view and then pass that into the child views so that they can watch for changes. Upon seeing a change in the selection, the child view can then reset a @State
variable that change the isActive
property of the NavigationLink
.
class NavigationManager : ObservableObject {
@Published var activeTab = 0
}
struct MyTabView: View {
@StateObject private var navigationManager = NavigationManager()
var body: some View {
TabView(selection: $navigationManager.activeTab) {
TabOne().tabItem { Image(systemName: "1.square") }.tag(0)
TabTwo().tabItem { Image(systemName: "2.square") }.tag(1)
}.environmentObject(navigationManager)
}
}
struct TabOne: View {
var body: some View {
Text("1")
}
}
struct TabTwo: View {
@EnvironmentObject private var navigationManager : NavigationManager
@State private var linkActive = false
var body: some View {
NavigationView {
NavigationLink("Go to sub view", isActive: $linkActive) {
TabTwoSub()
}
}.onChange(of: navigationManager.activeTab) { newValue in
linkActive = false
}
}
}
struct TabTwoSub: View {
var body: some View {
Text("Tapping \(Image(systemName: "1.square")) doesnt unwind this view back to the root of the NavigationView")
.multilineTextAlignment(.center)
}
}
Note: this will result in a "Unbalanced calls to begin/end appearance transitions" message in the console -- in my experience, this is not an error and not something we have to worry about
Related Topics
Use Different Googleservice-Info.Plist for Single Project in Xcode Using Swift4
Swift: Copy Information Selected by User in Abpersonviewcontroller to Dictionary
How to Check If Airpods Are Connected to Iphone
How to Make JSON Data Persistent for Offline Use (Swift 4)
Shared Cookies with Wkprocesspool for Wkwebview in Swift
How to Get a Double Value Up to 2 Decimal Places
Swift Enumeration Order and Comparison
Swiftui, Shadow Only for Container
Iterating Over an Nsorderedset
Gmsplace Returns Invalid Coordinate (-180, -180), But Name and Place Id Are Correct
Captureoutput Function Isn't Called Using Setsamplebufferdelegate
How to Access Cfdictionary in Swift 3
Create CSV File in Swift and Write to File
Swift 3/Macos: Open Window on Certain Screen
Swiftui: How to Implement Edit Menu in MACos App