Swift 中的 UIActivityViewController 在 iPad 时崩溃
UIActivityViewController in Swift Crashes on iPad
我正在我的游戏中创建分享功能,我有代码,它在 iPhone 上运行良好,但是当我在 iPad 上测试它时,当我点击分享按钮时,应用程序崩溃。我将以下代码用于共享按钮
let textToShare = "Check out this website!"
if let myWebsite = NSURL(string: "http://www.apple.com/") {
let objectsToShare = [textToShare, myWebsite]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
self.view?.window?.rootViewController?.presentViewController(activityVC, animated: true, completion: nil)
}
当 iPad 上的 运行 时,UIActivityViewController 具有非空 popoverPresentationController 属性。那么,请尝试以下。
if let wPPC = activityVC.popoverPresentationController {
wPPC.sourceView = some view
// or
wPPC.barButtonItem = some bar button item
}
presentViewController( activityVC, animated: true, completion: nil )
popoverPresentationController
sourceView
需要设置为当前视图。
let activityVC = UIActivityViewController(activityItems: [quoteController.attributedString, view.screenShot()], applicationActivities: [])
present(activityVC, animated: true)
activityVC.popoverPresentationController?.sourceView = view
基于@Satachito 的回答:作为 sourceView
,您可以在弹出窗口应指向的位置创建一个(不可见的)CGRect
,并在该方向设置箭头:
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
if UIDevice.current.userInterfaceIdiom == .pad {
activityVC.popoverPresentationController?.sourceView = UIApplication.shared.windows.first
activityVC.popoverPresentationController?.sourceRect = CGRect(x: 0, y: 0, width: 300, height: 350)
activityVC.popoverPresentationController?.permittedArrowDirections = [.left]
}
UIApplication.shared.windows.first?.rootViewController?.present(activityVC, animated: true, completion: nil)
我正在我的游戏中创建分享功能,我有代码,它在 iPhone 上运行良好,但是当我在 iPad 上测试它时,当我点击分享按钮时,应用程序崩溃。我将以下代码用于共享按钮
let textToShare = "Check out this website!"
if let myWebsite = NSURL(string: "http://www.apple.com/") {
let objectsToShare = [textToShare, myWebsite]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
self.view?.window?.rootViewController?.presentViewController(activityVC, animated: true, completion: nil)
}
当 iPad 上的 运行 时,UIActivityViewController 具有非空 popoverPresentationController 属性。那么,请尝试以下。
if let wPPC = activityVC.popoverPresentationController {
wPPC.sourceView = some view
// or
wPPC.barButtonItem = some bar button item
}
presentViewController( activityVC, animated: true, completion: nil )
popoverPresentationController
sourceView
需要设置为当前视图。
let activityVC = UIActivityViewController(activityItems: [quoteController.attributedString, view.screenShot()], applicationActivities: [])
present(activityVC, animated: true)
activityVC.popoverPresentationController?.sourceView = view
基于@Satachito 的回答:作为 sourceView
,您可以在弹出窗口应指向的位置创建一个(不可见的)CGRect
,并在该方向设置箭头:
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
if UIDevice.current.userInterfaceIdiom == .pad {
activityVC.popoverPresentationController?.sourceView = UIApplication.shared.windows.first
activityVC.popoverPresentationController?.sourceRect = CGRect(x: 0, y: 0, width: 300, height: 350)
activityVC.popoverPresentationController?.permittedArrowDirections = [.left]
}
UIApplication.shared.windows.first?.rootViewController?.present(activityVC, animated: true, completion: nil)