Using constraints to animate full screen UIImageView

918 Views Asked by At

In my iPhone(iOS) application, I am using an image which is full screen to show the "Help" about different options available in the screen. I set the leading, top, trailing and bottom constraint constants to "0" pointing to super view. This scenario is working good on iPhone 4S, 5/5C/5S, 6 and 6+ without any problem.
Now on tap of the image view, I've to animate the image going from right to left. On tapping "Help" button on the main view, the help image will come back from left to right animation. I'm using below code for animation:

self.helpViewLeadingConstraint.constant = 600;
[self.view layoutIfNeeded];


Problem I'm facing is that, when animation is in progress, I'm seeing some warnings.

    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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7fdda85a4b70 UIView:0x7fddaa8f05a0.trailingMargin == UIView:0x7fdda85a3460.trailing - 16>",
    "<NSLayoutConstraint:0x7fdda85a4c10 UIView:0x7fdda85a3460.leading == UIView:0x7fddaa8f05a0.leadingMargin + 600>",
    "<NSLayoutConstraint:0x7fddabb09e30 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fddaa8f05a0(375)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fdda85a4b70 UIView:0x7fddaa8f05a0.trailingMargin == UIView:0x7fdda85a3460.trailing - 16>

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


I couldn't find a solution to avoid the warning messages. How can we set two constraints satisfying at a time without generating any unsuccessful messages?

2

There are 2 best solutions below

1
On

Have you tried calling setNeedsUpdateConstraints prior to setting your constant? This might also be helpful https://github.com/SnapKit/SnapKit

0
On

The warning message is right... You have to update both leading and trailing constraints at the same time. Leading+600 and Trailing-600 this will retain image dimension. and not break the existing constraints.

In my case I have 2 fullscreen images, one is going up and one is going down at the same time.

Here is the code for the same:

func animateOverlayImage() {
    let viewHeight = self.view.frame.height
    UIView.animate(withDuration: Double(2.0), animations: {
        self.topImgTopConstraint.constant = -1 * viewHeight
        self.topImgBottomConstraint.constant = viewHeight

        self.bottomImgTopConstraint.constant = viewHeight
        self.bottomImgBottomConstraint.constant = -1 * viewHeight

        self.view.layoutIfNeeded()
    })
}