简体   繁体   English

无法通过 iPad 上 SwiftUI 中的 NavigationBarItem 呈现 ActionSheet

[英]Unable to present ActionSheet via a NavigationBarItem in SwiftUI on an iPad

First, I have looked at a similar question, but it does not address my use case.首先,我看过一个类似的问题,但它没有解决我的用例。

Present ActionSheet in SwiftUI on iPad 在 iPad 上的 SwiftUI 中呈现 ActionSheet

My issue is that I have a NavigationBarItem in my NavigationView that will toggle an ActionSheet when pressed.我的问题是我的NavigationBarItem中有一个NavigationView ,它会在按下时切换一个ActionSheet This behavior works properly when used on an iPhone.此行为在 iPhone 上使用时可以正常工作。

However, when I use this on an iPad, both buttons on my screen will gray out and nothing happens.但是,当我在 iPad 上使用它时,我屏幕上的两个按钮都会变灰,什么也没有发生。 Clicking the buttons again will make them active (blue), but again, no sheet is presented.再次单击按钮将使它们处于活动状态(蓝色),但同样不会显示任何工作表。

Finally, if I select the button in the middle of the screen (Show Button), then an ActionSheet is properly presented on an iPad.最后,如果我 select 是屏幕中间的按钮(显示按钮),那么在 iPad 上正确显示了一个 ActionSheet。

I have tested with Xcode 11 & iOS 13.5 and Xcode 12 & iOS 14. There is no change in behavior. I have tested with Xcode 11 & iOS 13.5 and Xcode 12 & iOS 14. There is no change in behavior.

import SwiftUI

struct ContentView: View {
    @State private var isButtonSheetPresented = false
    @State private var isNavButtonSheetPresented = false

    var body: some View {
        NavigationView {
            Button(action: {
                // Works on iPad & iPhone
                self.isButtonSheetPresented.toggle()
              }) {
                Text("Show Button")
            }
            .actionSheet(isPresented: $isButtonSheetPresented,
                         content: {
                             ActionSheet(title: Text("ActionSheet"))
              })
            .navigationBarTitle(Text("Title"),
                                displayMode: .inline)
            .navigationBarItems(trailing:
                Button(action: {
                    // Works on iPhone, fails on iPad
                    self.isNavButtonSheetPresented.toggle()
                    }) {
                    Text("Show Nav")
                }
                .actionSheet(isPresented: $isNavButtonSheetPresented,
                             content: {
                                 ActionSheet(title: Text("ActionSheet"))
                    })
            )
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

Finally, this is how it appears on an iPad when clicking on "Show Nav":最后,这是在单击“显示导航”时它在 iPad 上的显示方式:

在此处输入图像描述

This is a simplified setup for the screen where this issue occurs.这是发生此问题的屏幕的简化设置。 I will need to retain the navigation settings shown, but have included them for clarity.我需要保留显示的导航设置,但为了清楚起见已将它们包括在内。

*** UPDATED *** *** 更新 ***

While it is not possible for the real app behind this, I did remove the .navigationViewStyle(StackNavigationViewStyle()) setting, which did make an ActionSheet appear, although in the wrong spot as seen below.虽然这背后的真实应用程序不可能,但我确实删除了.navigationViewStyle(StackNavigationViewStyle())设置,它确实使 ActionSheet 出现,尽管在错误的位置,如下所示。

在此处输入图像描述

This also results in bizarre placement for the Button one accessed via "Show Button".这也导致通过“显示按钮”访问的按钮位置奇怪。

在此处输入图像描述

Yes, it is a bug, but probably different - that Apple does not allow to change anchor and direction of shown ActionSheet , because it is shown, but always to the right of originated control on iPad.是的,这是一个错误,但可能不同 - Apple 不允许更改显示的ActionSheet的锚点和方向,因为它已显示,但始终位于 iPad 上的原始控件的右侧。 To prove this it is enough to change location of button in Navigation为了证明这一点,改变导航中按钮的位置就足够了

Here is example of placing at .leading position.这是放置在.leading position 的示例。 Tested with Xcode 12 / iOS 14用 Xcode 12 / iOS 14 测试

演示

.navigationBarItems(leading:       
    Button(action: {
        // Works on iPhone, fails on iPad
        self.isNavButtonSheetPresented.toggle()
        }) {
        Text("Show Nav")
    }
    .actionSheet(isPresented: $isNavButtonSheetPresented,
                 content: {
                     ActionSheet(title: Text("ActionSheet"))
        })
)

Note: SwiftUI 2.0 .toolbar behaves in the same way, ie.注意: SwiftUI 2.0 .toolbar的行为方式相同,即。 has same bug.有同样的错误。

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

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