【问题标题】:iPad crashes when trying to share by taping a UIBarButtonItem尝试通过点击 UIBarButtonItem 进行共享时 iPad 崩溃
【发布时间】:2016-08-28 09:37:12
【问题描述】:

我正在使用 Xcode 和 Swift 开发 iOS 应用程序。

我正在使用此代码通过录制 UIButton 来共享定义的字符串:

@IBAction func shareApp(sender: UIButton) {
    let textToShare = "Look at this:"

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

        if #available(iOS 8.0, *) {
            activityVC.popoverPresentationController?.sourceView = sender
        } else {
            // Fallback on earlier versions
        }
        self.presentViewController(activityVC, animated: true, completion: nil)
    }
}

这在 iPhone 和 iPad 上运行良好。

现在,我想做同样的事情,但使用UIBarButtonItem 而不是UIButton。当然,此代码不适用于 UIBarButtonItem。

只要我点击 BarButtonItem,更改 @IBAction func shareApp(sender: AnyObject) {activityVC.popoverPresentationController?.sourceView = sender as? UIView 就会导致 iPad's 崩溃。

有人知道怎么解决吗?

【问题讨论】:

    标签: ios swift ipad uiactivityviewcontroller


    【解决方案1】:

    对于 iPad,除了 popoverPresentationController 的源视图之外,您还需要添加 sourceRect。 请尝试以下操作:

     if ( UIDevice.currentDevice().userInterfaceIdiom == .Pad) {
    
         if let fromView = sender as? UIView {
                  youActivityVC.popoverPresentationController.sourceView = fromView
    
        youActivityVC.popoverPresentationController.sourceRect = fromView.frame
    }
    }
    

    【讨论】:

    • 我会检查并返回。
    • 嗨@luckystars,很抱歉我的回复晚了。您的解决方案没有帮助。我更新了我的问题。请看一下。你能帮帮我吗?
    猜你喜欢
    • 2018-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 2018-11-13
    • 2015-08-17
    • 1970-01-01
    相关资源
    最近更新 更多