Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI: Tap target for Navigation Bar Button misaligned after dismissing a sheet

Tags:

ios

swift

swiftui

I am noticing a rather strange, possibly buggy behavior in SwiftUI using iOS 13.3.1 and earlier. To demonstrate, this very simple scenario:

Full sample code:

import SwiftUI

struct ContentView: View {
    @State var showingSheet: Bool = false
    var body: some View {
        NavigationView {
            Text("ParentView")
                .navigationBarItems(trailing: Button(action: { self.showingSheet = true }) { Text("Show") })
        }.sheet(isPresented: $showingSheet) { SheetView() }
    }
}

struct SheetView: View {
    @Environment(\.presentationMode) var presentationMode
    var body: some View {
        VStack {
            Text("Sheet content")
            Button(action: { self.presentationMode.wrappedValue.dismiss() }) { Text("Hide") }
        }
    }
}

To reproduce:

  1. Launch the app on iOS 13.4.1
  2. Tap the "Show" button
  3. (The sheet shows, as expected)
  4. Dismiss the sheet using the "Hide" button
  5. (The sheet is dismissed, as expected)
  6. Try to show the sheet again, using the "Show" button

Expected result: The sheet shows again.

Actual result: The tap target of the "Show" button is shifted some pixels to the bottom, resulting in the action not triggering. This can also be observed in the View Hierarchy Debugger:

Misaligned Views

Other observations:

  • This does not happen when dismissing the sheet by dragging it to the bottom of the screen
  • This occurs only if large navigation item titles are activated for the hosting view, it's fine when using an inline title

So now I'm here wondering: Am I making a mistake, or is this an actual SwiftUI bug? If so, is there an easy workaround? I searched SO for similar issues, but didn't come across any that were identical.

Edit: I filed this with Apple as Radar Feedback FB7560960 in the meantime.

Edit 2: Still happening in 13.4.1.

like image 954
Joshua Avatar asked Feb 02 '20 12:02

Joshua


1 Answers

I don't know why, but adding @Environment(\.presentationMode) var presentationMode to contentView seems to fix it.

like image 67
DoNNy G Avatar answered Oct 25 '22 23:10

DoNNy G