The following code can make the running app unresponsive on iPhone (not iPad) or iPhone simulator. Xcode shows that the app consumes 100% CPU while allocating more and more memory.
struct SecondView: View { @State private var keyboardHeight: CGFloat = 0 private let showPublisher = NotificationCenter.Publisher.init( center: .default, name: UIResponder.keyboardWillShowNotification ).map { (notification) -> CGFloat in if let rect = notification.userInfo?["UIKeyboardFrameEndUserInfoKey"] as? CGRect { return rect.size.height } else { return 0 } } var body: some View { VStack(spacing: 20) { if keyboardHeight == 0 { Text("This is shown as long as there's no keyboard") } Text("This is the SecondView. Drag from the left edge to navigate back, but don't complete the gesture: crash results.") }.onReceive(self.showPublisher) { (height) in self.keyboardHeight = height } .navigationBarItems(trailing: Button("Dummy") { }) }}struct ContentView: View { @State var textInput = "" var body: some View { NavigationView { VStack(spacing: 20) { TextField("1. Tap here to show keyboard", text: self.$textInput) .textFieldStyle(RoundedBorderTextFieldStyle()) NavigationLink(destination: SecondView()) { Text("2. Go to second screen") } Spacer() } } }}
To trigger the freeze:
- Tap the textfield to make the keyboard appear
- Tap the link to go to the next screen
- Drag from the left side of the screen, but don't complete the gesture and instead release early
There are some workarounds:
- Remove the navigation bar item (the Dummy button) in
SecondView
- Remove the use of the
keyboardHeight
variable inSecondView
- Don't activate the keyboard in
ContentView
before navigating
However, I can't use the above workarounds in my app. Does anyone know what the root cause is?