【问题标题】:UIActivityViewController becomes blank on iOS 15UIActivityViewController 在 iOS 15 上变为空白
【发布时间】:2021-11-28 10:43:11
【问题描述】:

我有一个类似于 Apple 的照片应用程序的 iOS 应用程序,它还有一个用于分享照片的“分享”按钮,之前效果很好。代码如下(为简单起见,我将分享内容改为字符串):

@objc func shareButtonTapped()
{
    let vc = UIActivityViewController(activityItems: ["www.apple.com"], applicationActivities: nil);
    if let pop = vc.popoverPresentationController
    {
        pop.sourceView = someView;
        pop.sourceRect = shareButton.frame;
    }
    self.present(vc, animated: true, completion: nil);
}

但是当我的 iPhone 升级到 iOS 15 时,显示的 UIActivityViewController 是不可见的。我附上一个操作视频: enter link description here 仔细观察,其实 UIActivityViewController 是有弹窗的,只不过已经变得几乎透明了。

然后,我添加了一条语句来故意设置其视图的背景颜色:

@objc func shareButtonTapped()
{
    let vc = UIActivityViewController(activityItems: ["www.apple.com"], applicationActivities: nil);
    vc.view.backgroundColor = UIColor.systemBackground;
    if let pop = vc.popoverPresentationController
    {
        pop.sourceView = someView;
        pop.sourceRect = shareButton.frame;
    }
    self.present(vc, animated: true, completion: nil);
}

操作视频如下: enter link description here

这段代码很简单也很标准。我不知道为什么会这样? 希望有人可以提供帮助。提前致谢!


实际上,我在派生自 UICollectionViewCell 的类中定义了 shareButton:

class AssetPreviewCell: UICollectionViewCell, UIScrollViewDelegate, PHLiveViewDelegate, UINavigationControllerDelegate
{
    //....
}

完整的代码是:

@objc func shareButtonTapped()
{
    guard let svc = self.findViewController() else { return }

    let vc = UIActivityViewController(activityItems: ["www.apple.com"], applicationActivities: nil);
    if let pop = vc.popoverPresentationController
    {
        pop.sourceView = someView;
        pop.sourceRect = shareButton.frame;
    }
    svc.present(vc, animated: true, completion: nil);
}

func findViewController() 是来自enter link description here的方法


编辑:

我在“分享”按钮旁边有一个“相册”按钮。当点击“相册”按钮时,我会显示另一个视图控制器,用于根据用户选择或取消选择将当前照片添加到相册或从相册中删除当前照片。此视图控制器正常显示为安静。操作视频在enter link description here。所以我认为问题只是来自 UIActivityViewController 或其他东西。

【问题讨论】:

  • self 是正确的UIViewController 并且您不是试图让 Window/RootVC 显示它吗? stackoverflow.com/questions/68387187/…
  • @Larme,感谢您的评论。我已经编辑了我的问题。
  • 好的,所以findViewController 可能是罪魁祸首...我想如果您在显示collectionView 的UIViewController 中尝试您的初始代码,您不会有问题吗?你能打印svc吗?是你认为应该呈现共享的 ViewController 吗?
  • @Larme ,我打印了 svc,它应该是正确的 ViewController。
  • @Fattie ,我刚才在iOS 15.0的iPhone 13 mini模拟器上测试过,同样报错。

标签: swift uiactivityviewcontroller ios15


【解决方案1】:

我找到了答案。问题是因为我因为某种原因覆盖了 UIActivityViewController 的 viewDidLoad 函数:

override open func viewDidLoad()
{
    super.viewDidLoad();
    a_var += 1;
}

这会在 iOS15 上导致问题,而在 iOS14 或 iOS13 上运行良好。

现在我改写 viewDidAppear(_) 并且问题消失了:

override open func viewDidAppear(_ animated: Bool)
{
    super.viewDidAppear(animated);
    a_var += 1;
}

ps:@mczmma 是我的另一个帐户。此帐户仅限提问。不知道是什么原因。

【讨论】:

  • 原帖中没有覆盖,能否请您在原始代码的上下文中发布您的解决方案?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-18
  • 1970-01-01
  • 2021-11-18
  • 1970-01-01
  • 2014-10-27
相关资源
最近更新 更多