3

我尝试调用 popoverpresentation 视图控制器以在 ipad 上共享图像,但是当发生这种情况时,我收到错误“无法满足约束”。

问题是为了解决这个问题,我删除了所有约束,所以我可以重新开始,但即使没有约束,我也会遇到同样的错误。

所以我的问题是。这是一个错误还是我必须为 popoverpresentation 视图控制器设置约束。

这是我的代码:

print("Current device is an iPad")
print("Current device is \(UIDevice.current)")

if let imageCheck = image {
    let imageToShare = [imageCheck]
    let activityVC = UIActivityViewController(activityItems: imageToShare, applicationActivities: nil)

    activityVC.popoverPresentationController?.sourceView = super.view
    self.present(activityVC, animated: true, completion: nil)
}

控制台中的错误:

Current device is an iPad
Current device is <UIDevice: 0x280c8d260>
2019-12-26 20:31:22.662290+0100 Petfie[2184:756367] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x282f83c00 LPLinkView:0x15be4c7a0.leading == UILayoutGuide:0x2835a8c40'UIViewLayoutMarginsGuide'.leading   (active)>",
    "<NSLayoutConstraint:0x282f83840 H:[LPLinkView:0x15be4c7a0]-(59)-|   (active, names: '|':_UIActivityContentTitleView:0x15be47560 )>",
    "<NSLayoutConstraint:0x282f85d10 H:|-(0)-[_UIActivityContentTitleView:0x15be47560]   (active, names: '|':_UINavigationBarContentView:0x15be57940 )>",
    "<NSLayoutConstraint:0x282f85d60 _UIActivityContentTitleView:0x15be47560.trailing == _UINavigationBarContentView:0x15be57940.trailing   (active)>",
    "<NSLayoutConstraint:0x282f9eb20 'UIView-Encapsulated-Layout-Width' _UINavigationBarContentView:0x15be57940.width == 0   (active)>",
    "<NSLayoutConstraint:0x282f83b10 'UIView-leftMargin-guide-constraint' H:|-(16)-[UILayoutGuide:0x2835a8c40'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':_UIActivityContentTitleView:0x15be47560 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x282f83c00 LPLinkView:0x15be4c7a0.leading == UILayoutGuide:0x2835a8c40'UIViewLayoutMarginsGuide'.leading   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
4

1 回答 1

7

我刚刚在我自己的项目中解决了同样的问题。

在 iPad 上显示 UIActivityViewController 时,要么需要设置sourceViewANDsourceRect属性,要么需要设置barButtonItem属性。

在我的项目中,我通过将以下代码添加到在shareButton被点击时运行的块中来解决此问题:

activityViewController.popoverPresentationController?.sourceView = self.view
activityViewController.popoverPresentationController?.sourceRect = shareButton.frame

sourceRectUIActivityViewController“弹出”的CGRect。遗嘱上的箭头UIActivityViewController指向sourceRect

于 2020-05-05T19:31:19.277 回答