gpt4 book ai didi

ios - UIActivity activityViewController 在 Swift 中的 iPad 上崩溃

转载 作者:可可西里 更新时间:2023-11-01 01:42:09 32 4
gpt4 key购买 nike

我有下面的代码来切换 UIActivityViewController

@IBAction func shareButtonClicked(sender: UIButton)
{
let textToShare = "Text"

if let myWebsite = NSURL(string: "http://www.example.com/")
{
let objectsToShare = [textToShare, myWebsite]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

self.presentViewController(activityVC, animated: true, completion: nil)
}
}

但它在 iPad 上崩溃了。我如何在 Swift 中以模态方式呈现它?

最佳答案

它会崩溃,因为在 iPad 中你不能像在 iPhone 中那样呈现 UIActivityViewController

您需要按照 Apple 建议的设计指南将其呈现在弹出窗口中。

下面的代码将演示如何在 UIPopoverPresentationController 中显示它。

@IBAction func shareButtonClicked(sender: UIButton)
{
let textToShare = "Text"

if let myWebsite = NSURL(string: "http://www.example.com/")
{
let objectsToShare = [textToShare, myWebsite]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

var nav = UINavigationController(rootViewController: activityVC)
nav.modalPresentationStyle = UIModalPresentationStyle.Popover
var popover = nav.popoverPresentationController as UIPopoverPresentationController!
activityVC.preferredContentSize = CGSizeMake(500,600)
popover.sourceView = self.view
popover.sourceRect = CGRectMake(100,100,0,0)

self.presentViewController(nav, animated: true, completion: nil)

}
}

注意:使用 UIPopoverPresentationController 委托(delegate)扩展您的 View Controller 。

例如:

class MyViewController : UIViewController, UIPopoverPresentationControllerDelegate {
//........
}

关于ios - UIActivity activityViewController 在 Swift 中的 iPad 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28759365/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com