【问题标题】:SwiftUI Share Sheet Crashes iPadSwiftUI Share Sheet 让 iPad 崩溃
【发布时间】:2021-04-13 16:21:24
【问题描述】:
【问题讨论】:
标签:
swift
swiftui
swift5
uiactionsheet
【解决方案1】:
尝试使用此代码在 iPad 中打开 Actionsheet。
if let vc = UIApplication.shared.windows.first?.rootViewController{
let activityVC = UIActivityViewController(activityItems: [urlShare], applicationActivities: nil)
if isIpad{
activityVC.popoverPresentationController?.sourceView = vc.view
activityVC.popoverPresentationController?.sourceRect = .zero
}
UIApplication.shared.windows.first?.rootViewController?.present(activityVC, animated: true, completion: nil)
}
【解决方案2】:
您只需在UIActivityViewController 的popoverPresentationController 上设置sourceView 和sourceRect。这是一个比我迄今为止看到的更完整和正确的示例:
// Get a scene that's showing (iPad can have many instances of the same app, some in the background)
let activeScene = UIApplication.shared.connectedScenes.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene
let rootViewController = (activeScene?.windows ?? []).first(where: { $0.isKeyWindow })?.rootViewController
// iPad stuff (fine to leave this in for all iOS devices, it will be effectively ignored when not needed)
activityVC.popoverPresentationController?.sourceView = rootViewController?.view
activityVC.popoverPresentationController?.sourceRect = .zero
rootViewController?.present(activityVC, animated: true, completion: nil)