在iPad上的SwiftUI中呈现ActionSheet

MSc*_*ler 17 ipad swift swiftui

我已经有了一个ActionSheet,可以在iPhone设备上正常显示。但它在iPad上崩溃。说它需要弹出窗口的位置。有人用过此代码吗?我正在使用iOS 13 beta 3和Xcode 11 beta3。(这使用的是一个表示ActionSheet的版本,该版本在beta 2中不可用)

import SwiftUI

struct ContentView : View {
    @State var showSheet = false

    var body: some View {
        VStack {
            Button(action: {
                self.showSheet.toggle()
            }) {
                Text("Show")
            }
            .presentation($showSheet) { () -> ActionSheet in
                ActionSheet(title: Text("Hello"))

            }
        }
    }
}

#if DEBUG
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif
Run Code Online (Sandbox Code Playgroud)

MSc*_*ler 9

最后,正如在 iOS 13.4 中测试的那样,这已经得到解决,至少在测试版中是这样。冲突约束警告仍然存在,但崩溃消失了。现在这是展示行动表的适当方式。

import SwiftUI

struct ContentView : View {
    @State var showSheet = false

    var body: some View {
        VStack {
            Button(action: {
                self.showSheet.toggle()
            }) {
                Text("Show")
            }
            .actionSheet(isPresented: $showSheet, content: { ActionSheet(title: Text("Hello"))
            })
        }
    }
}

struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 刚刚在运行 iOS 13.4 的 iPad 模拟器上进行了测试,没有任何显示,只有“[LayoutConstraints]无法同时满足约束”错误。当然,操作表在 iPhone (iOS 13.4) 上显示良好 (2认同)

mar*_*rux 5

遗憾的是,此错误尚未针对iOS 13的最终版本进行修复。在开发人员论坛上已提及该错误,我已经对此进行了反馈(FB7397761),但暂时需要使用一些解决方法。其他UI时UIDevice.current.userInterfaceIdiom == .pad

作为记录,(无用的)异常消息是:

2019-10-21 11:26:58.205533-0400 LOOksTape[34365:1769883] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController (<UIAlertController: 0x7f826e094a00>) of style UIAlertControllerStyleActionSheet from _TtGC7SwiftUI19UIHostingController… 
The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. 
You must provide location information for this popover through the alert controller's popoverPresentationController. 
You must provide either a sourceView and sourceRect or a barButtonItem.  
If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'

Run Code Online (Sandbox Code Playgroud)

解决方法是,此popSheet功能将在iPad和ActionSheet其他任何地方显示弹出框:

2019-10-21 11:26:58.205533-0400 LOOksTape[34365:1769883] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController (<UIAlertController: 0x7f826e094a00>) of style UIAlertControllerStyleActionSheet from _TtGC7SwiftUI19UIHostingController… 
The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. 
You must provide location information for this popover through the alert controller's popoverPresentationController. 
You must provide either a sourceView and sourceRect or a barButtonItem.  
If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'

Run Code Online (Sandbox Code Playgroud)