iPad 上的 UIActivityViewController

UIActivityViewController on iPad

我一直在使用下面的代码来显示 UIActivityViewController,当我使用 Xcode 6、Swift 1.2 和 iOS 8 时它工作正常。但是当我更新它显示 UIActivityViewController 但它完全空白,没有任何共享选项。你有什么建议吗?

if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
        let textToShare = textViewOne.text

            let objectsToShare = [textToShare]
            let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

            let nav = UINavigationController(rootViewController: activityVC)
            nav.modalPresentationStyle = UIModalPresentationStyle.Popover
            let popover = nav.popoverPresentationController as UIPopoverPresentationController!

            popover.sourceView = self.view
            popover.sourceRect = sender.frame

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

    } else {
        let textToShare = textViewOne.text

        let objectsToShare = [textToShare]
        let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

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

    }

这已经解决了。

        let objectsToShare = [textToShare]
        let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
        activityVC.title = "Share One"
        activityVC.excludedActivityTypes = []

        activityVC.popoverPresentationController?.sourceView = self.view
        activityVC.popoverPresentationController?.sourceRect = sender.frame

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

在 swift 3.0:

let objectsToShare = [textToShare]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.title = "Share One"
activityVC.excludedActivityTypes = []

activityVC.popoverPresentationController?.sourceView = self.view
activityVC.popoverPresentationController?.sourceRect = sender.frame

self.present(activityVC, animated: true, completion: nil)

Swift 4.0

    let shareText = "Hi"
    let activity = UIActivityViewController(activityItems: shareText, applicationActivities: nil)
    activity.excludedActivityTypes = []

    if UIDevice.current.userInterfaceIdiom == .pad {
        activity.popoverPresentationController?.sourceView = self.view
        activity.popoverPresentationController?.sourceRect = sender.frame
    }
    self.present(activity, animated: true, completion: nil)

我一直在为 SWIFT 5 的上述建议而苦苦挣扎,因为:

activity.popoverPresentationController?.sourceRect = sender.frame

在某些情况下不起作用,因为发件人在范围内不可用。 尝试使用 CGRECT 进行设置,例如:

activityController.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY,width: 0,height: 0)

我希望这对一些人有所帮助。

最好解决 iPad 和 iPhone 尝试一下

let urlstring = "https://apps.apple.com/ae/app/"
let text = "some text for your app"
let url = NSURL(string: urlstring)
let textToShare = [url!,text] as [Any]
let activityViewController = UIActivityViewController(activityItems: textToShare as [Any], applicationActivities: nil)
    
activityViewController.excludedActivityTypes = [ UIActivity.ActivityType.airDrop, UIActivity.ActivityType.postToFacebook ,UIActivity.ActivityType.postToFlickr,UIActivity.ActivityType.postToTwitter,UIActivity.ActivityType.postToVimeo,UIActivity.ActivityType.mail,UIActivity.ActivityType.addToReadingList]

activityViewController.popoverPresentationController?.sourceView = self.view
activityViewController.popoverPresentationController?.sourceRect = view.bounds
activityViewController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.down
UIApplication.shared.windows.first?.rootViewController?.present(activityViewController, animated: true, completion: nil)

我遇到了一些不同的问题。我希望 UIActivityViewController 保持在屏幕中央,但是在旋转 iPad 时它在风景中偏离中心。

横向错误:

2 个修复是使 UIActivityViewController 成为 class 属性,最重要的是在 viewDidLayoutSubviews 中设置它的 sourceRect。按照5个步骤-

// 1. make the activityVC a class property
var activityVC: UIActivityViewController?

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

    if UIDevice.current.userInterfaceIdiom == .pad {
        // 2. set its sourceRect here. It's the same as in step 4           
        activityVC?.popoverPresentationController?.sourceRect = CGRect(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height / 2, width: 0, height: 0)
    }
}

// 3. present the UIActivityViewController
func presentActivityVC() {

    let objectsToShare = [textToShare]

    activityVC = nil
    activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
    activityVC?.excludedActivityTypes = [.addToReadingList, .openInIBooks, .print]
    activityVC?.popoverPresentationController?.sourceView = self.view

    if UIDevice.current.userInterfaceIdiom == .phone {
        activityVC?.modalPresentationStyle = .overFullScreen
    }
        
    if UIDevice.current.userInterfaceIdiom == .pad {
        // 4. set its sourceRect here. It's the same as in step 2
        activityVC?.popoverPresentationController?.sourceRect = CGRect(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height / 2, width: 0, height: 0)
        activityVC?.popoverPresentationController?.permittedArrowDirections = []
    }

    present(activityVC!, animated: true, completion: nil)

    activityVC?.completionWithItemsHandler = { [weak self](activityType, completed:Bool, returnedItems:[Any]?, error: Error?) in
        if let error = error {
            print(error.localizedDescription)
            return
        }

        // 5. set the activityVC to nil after the user is done 
        DispatchQueue.main.async { [weak self] in
            self?.activityVC = nil
        }
    }
}

现在旋转时它在横向和纵向都居中。

横向正确:

纵向:

如果您想将弹出窗口附加到发送者视图的边界,则以下代码将起作用:

let vc = UIActivityViewController(activityItems: [url], applicationActivities: nil)
if UIDevice.current.userInterfaceIdiom == .pad {
  vc.popoverPresentationController?.sourceView = sender
  vc.popoverPresentationController?.sourceRect = CGRect(origin: CGPoint.zero, size: sender.frame.size)
}

sourceRect定义在sourceView的坐标space,因此我们需要指定sourceRect的原点为CGPoint.zero而不是sender.frame.origin.

我们需要专门为iPad设置sourceView和sourceRect。

我们可以试试下面的代码片段

activityViewController.popoverPresentationController?.sourceView = sender.self
activityViewController.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.minX + sender.frame.width/2, y: self.view.bounds.minY, width: 0, height: 0)
activityViewController.popoverPresentationController?.permittedArrowDirections = []

它会将 sourceView 设置为发件人的,并将 sourceRect 设置在 UI 的中心。

我们正在将 sender.frame.width/2 添加到 x 坐标并删除锚箭头,以使 pop-up 正好位于中心。