4

The following code reproduced the error:

import SwiftUI

struct ContentView: View {
    @State private var number: Int = 5
    var body: some View {
        NavigationView() {
            VStack(spacing: 20) {
                NavigationLink(destination: SecondView(bottles: $number)) {
                    Text("Click me")
                }
            }
        }
    }
}

struct SecondView: View {
    @Environment(\.presentationMode) private var presentationMode: Binding<PresentationMode>
    @State private var color: UIColor = .black
    @Binding var bottles: Int

    var body: some View {
        Text("I have \(bottles) in my bag")
            .foregroundColor(Color(color))
            .navigationBarTitle(Text("Water Bottle"))
            .navigationBarItems(trailing:
                Button("Click") {
                    self.someFunction()
                }
        )
    }

    func someFunction() {
        if self.color == UIColor.black {
            self.color = .red
        } else {
            self.color = .black
        }
    }
}

When sliding back from SecondView to ContentView but didn't complete the gesture, the app freezes. When deleting either @Environment or NavigationBarItem will fix this error.

For @Environment, it is needed for CoreData but used presentationMode for reproduction of error

Boom PT
  • 374
  • 4
  • 11
  • Seems related to this: https://stackoverflow.com/questions/59344756/swiftui-navigationbaritems-slideback-freezes-app – New Dev Jun 03 '20 at 11:35
  • Yes but mine doesn’t have either .sheet and .alert modifier – Boom PT Jun 03 '20 at 12:21
  • I found out that if I change the navigation bar title into an inline display, there is will be no crashes, however, the title will stuck at the top of the view until refreshed – Boom PT Jun 05 '20 at 07:54
  • 1
    it works if you add ".navigationViewStyle(StackNavigationViewStyle())" to the NavigationView() in ContentView. But crashes if it is "DefaultNavigationViewStyle" – workingdog support Ukraine Jun 05 '20 at 08:48

1 Answers1

5

adding ".navigationViewStyle(StackNavigationViewStyle())" to the NavigationView fix the problem for me. This is the code I use for testing this on real devices (iPhone, iPad) and various simulators. Using macos 10.15.5, Xcode 11.5 and 11.6 beta, target ios 13.5 and mac catalyst.

I have not tested this on all devices, so let me know if you find a device where this does not work.

import SwiftUI

struct ContentView: View {
@State private var number: Int = 5
var body: some View {
    NavigationView() {
        VStack(spacing: 20) {
            NavigationLink(destination: SecondView(bottles: $number)) {
                Text("Click me")
            }
        }
    }.navigationViewStyle(StackNavigationViewStyle())  //  <---
}
}

struct SecondView: View {
@Environment(\.presentationMode) private var presentationMode: Binding<PresentationMode>
@State private var color: UIColor = .black
@Binding var bottles: Int

var body: some View {
    Text("I have \(bottles) in my bag")
        .foregroundColor(Color(color))
        .navigationBarTitle(Text("Water Bottle"))
        .navigationBarItems(trailing:
            Button("Click") {
                self.someFunction()
            }
    )
}

func someFunction() {
    if self.color == UIColor.black {
        self.color = .red
    } else {
        self.color = .black
    }
}
}