Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Popoverpresentation viewcontroller and constraint trouble

I try to invoke a popoverpresentation viewcontroller in order to share an image on an ipad, but when that happens I get the error 'unable to satisfy constraints'.

The thing is in order to fix this I deleted all constraints so I could start over, but even with no constraints I get the same error.

So my question is. Is this a bug or do I have to set the constraints for the popoverpresentation viewcontroller.

Here is my code:

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)
}

And the error in the console :

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.
like image 541
Martijn Avatar asked Oct 28 '25 05:10

Martijn


1 Answers

I just fixed the same problem on my own project.

When showing a UIActivityViewController on iPad, you either need to set the sourceView AND sourceRect properties, or you need to set the barButtonItem property.

On my project, I fixed the issue by adding the following code to the block that runs when the shareButton is tapped:

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

The sourceRect is the CGRect that the the UIActivityViewController "pops out" of. The arrow on the UIActivityViewController will point to the sourceRect

like image 53
ab1470 Avatar answered Oct 29 '25 22:10

ab1470