Remove Auto-Layout Constraints for Specific Object

I agree Xcode auto-layout is horrible to use if you want something particular...

What I use to remove constraints programatically:

[detailsPhotoView removeConstraints:detailsPhotoView.constraints];

In XCode 5 there is an option while editing constraints 'Remove at build time'. If this is enabled the constraint will automatically be removed during run-time. The option is there to keep Interface Builder happy if you don't want particular constraints.


There are two distinct issues here:

  1. How to use Xcode to setup a UIImageView without constraints
  2. How to allow the UIImageView to be panned and/or resized when its layout is handled by constraints.

Regarding (1), jturton is correct. As long as you have enabled auto layout within Xcode, Xcode will guarantee/require that there are sufficient constraints to uniquely determine the size and location of your view.

If you have not manually specified enough constraints in Xcode (via the "user constraints" that appear in blue in the layout inspector), then Xcode will add new constraints that it guesses (which appear in purple) until there are sufficient constraints to determine the layout.

If the UIImageView had no constraints on it, its layout would not be determined, so you cannot set that up in Xcode. To fix this, what you should do is use Xcode to setup IBOutlets to all the constraints you want to remove, and then in your UIView's awakeFromNib: or in your UIViewController's viewDidLoad: you manually remove the constraints by calling removeConstraints:.

However, now your UIImageView's layout is underdetermined. So at this point you will want to manually add new constraints that will allow you to move and resize the UIImageView, which brings us to question (2).

Regarding (2), how to resize & move a view whose layout is determined by constraints, the answer is to setup gesture recognizers as usual to detect the pinch & pan gestures, but instead of those gesture recognizers calling setFrame: to modify the view, they should modify the constant parameter of a NSLayoutConstraint that determines the frame of the UIImageView and then call layoutIfNeeded:, which will cause the layout system to immediately translate that modified constraint into a new frame.

So, for instance, suppose you want to manipulate the UIImageView with a set of layout constraints that were very similar to familiar setFrame: calls. In that case, after removing the constraints setup by Xcode, you could add constraints to the view that specify its width, its height, and its space from the top and left of the superview. Then the action handler for your pan gesture recognizer, would just update the constant parameter of the pacer gesture recognizer, and your pinch gesture recognizer could just update the constant parameter of the height and width constraints.

Another way to do (2), which is the same under the hood but might be easier in practice, is to set translatesAutoresizingMaskIntoConstraints=YES. This will do two things. It will cause the auto layout system to automatically generate constraints in the view's superview based on the view's autoresizingMask. Second – crucially! – it will cause the layout system to automatically translate calls to setFrame: into modifications of those constraints.


You can't not have constraints - you must have enough constraints in place to unambiguously size and position every view

You can create a resizable view that is laid out using Autolayout.

For example, in your case, you can create positioning constraints - say, pin the centre of the image view horizontally and vertically in the centre of its superview.

You can also create size constraints, horizontally and vertically. See my answer here for how to do this. Create outlets to these two constraints (if using interface builder). You can have height and width constraints of a specific number, or use the multiplier to make a fixed aspect ratio.

In response to pinch gestures, you can adjust the .constant properties of the two size constraints, and call setNeedsLayout. This will resize your image view whilst keeping its centre pinned.

If you do the fixed aspect ratio suggestion above, resizing a view using constraints is actually much simpler than setting the frame.


you can call

[yourElement removeFromSuperview];

and then call

[yourSuperview addSubview:yourElement];

to remove all constraints from yourElement and add it back to the view.

It is worth mentioning that if you are doing the above on something like a UIView category or extension, you need to save a reference to the view's superview before removing from superview:

var theSuperview = self.superview
self.removeFromSuperview()
theSuperview?.addSubview(self)

You might also need to make yourElement strong instead of weak since removing from superview could potentially cause you to lose a reference to it.