【发布时间】: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