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

标签 ios ipad swift uipopovercontroller uiactivityviewcontroller

我有下面的代码来切换 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/

相关文章:

iphone - 分发使用其他代码的自定义 View

objective-c - 基于 iOS 页面的应用程序清除颜色问题

ios - 如果我的 View Controller 不可滚动,我可以执行 'pull to refresh' 操作吗?

javascript - 从 QR Contact 等网站将联系人数据保存到手机

ios - 在 Firebase 中查询嵌套元素

iphone - NSPredicate 过滤器

ios - 半透明为 false 时 UIToolBar 上的 Bar Tint

Swift:协变覆盖?

ios - 快速上传视频到服务器

ios - 登录xcode成功后跳转到另一个页面