简体   繁体   English

SwiftUI:关闭工作表后导航栏按钮的点击目标未对齐

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

I am noticing a rather strange, possibly buggy behavior in SwiftUI using iOS 13.3.1 and earlier.我注意到使用 iOS 13.3.1 及更早版本的 SwiftUI 中存在一个相当奇怪的、可能有错误的行为。 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在 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:这也可以在 View Hierarchy Debugger 中观察到:

未对齐的视图

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?所以现在我想知道:我是在犯错误,还是这是一个实际的 SwiftUI 错误? If so, is there an easy workaround?如果是这样,是否有简单的解决方法? I searched SO for similar issues, but didn't come across any that were identical.我在 SO 中搜索了类似的问题,但没有遇到任何相同的问题。

Edit: I filed this with Apple as Radar Feedback FB7560960 in the meantime.编辑:同时,我将此作为 Radar Feedback FB7560960 提交给了 Apple。

Edit 2: Still happening in 13.4.1.编辑 2:仍在 13.4.1 中发生。

我不知道为什么,但是将@Environment(\\.presentationMode) var presentationMode添加到contentView似乎可以解决它。

I had exactly the same issue, but funnily enough, was only able to reproduce it on some simulators/devices running iOS 14.4 and 15.0 - not on iOS 13.3.我有完全相同的问题,但有趣的是,只能在运行 iOS 14.4 和 15.0 的一些模拟器/设备上重现它 - 而不是在 iOS 13.3 上。

The only workaround that solved it for me was to change the presentation mode from .automatic (ie .pageSheet on iOS >=13) to .fullScreen (the previous default).为我解决它的唯一解决方法是将演示模式从.automatic (即.pageSheet on iOS >=13)更改为.fullScreen (以前的默认值)。 I suppose the equivalent in pure SwiftUI would be to use .fullScreenCover instead of .sheet .我想纯 SwiftUI 中的等价物是使用.fullScreenCover而不是.sheet

iOS 16+ iOS 16+

.sheet(isPresented: $showingSheet) {
  SheetView()
   .presentationDetents([.fraction(0.999)])
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM