Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temporarily remove NSLayoutConstraint?

I have a UITableViewCell subclass laid out using AutoLayout, and I’d like a small animation in which the image view transforms, grows a bit and shrinks back.

At present, due to the constraints, the labels on the right side of the image are moving too (as they should be).

I’d like a quick and easy means to temporarily say “leave those labels where they currently are while this animation is running”. Is that something I can do without needing to remove and re-add those constraints, which is a lot of hassle?

like image 787
Luke Avatar asked Jul 07 '14 10:07

Luke


People also ask

How to prevent invalid constraints in nslayoutconstraint?

This helps prevent the accidental creation of invalid constraints. For example, you can constrain horizontal anchors ( leadingAnchor or trailingAnchor) only with other horizontal anchors. Similarly, you can provide multipliers only for size constraints. These rules are not enforced by the NSLayoutConstraint API.

What is nslayoutattribute in iOS with example?

In iOS, the NSLayoutAttribute enumeration contains values for the view’s margins. This means that you can create constraints to the margins without going through the layoutMarginsGuide property. However, you still need to use the readableContentGuide property for constraints to the readable content guides.

What are layout anchors in nslayoutconstraint?

These rules are not enforced by the NSLayoutConstraint API. Instead, if you create an invalid constraint, that constraint throws an exception at runtime. Layout anchors, therefore, help convert runtime errors into compile time errors. For more information, see NSLayoutAnchor Class Reference .

What is a layout constraint?

The layout constraint orientation, either horizontal or vertical, that the constraint uses to enforce layout between objects. Keys that specify a horizontal or vertical layout constraint between objects.


2 Answers

Starting in iOS 8 NSLayoutConstraints now have an active property that you can change myConstraint.active = NO;. This removes the constraint, while setting this value to YES will [re]add it:

Activating or deactivating the constraint calls addConstraint: and removeConstraint: on the view that is the closest common ancestor of the items managed by this constraint. Use this property instead of calling addConstraint: or removeConstraint: directly. source

Additionally, there are two new class methods activateConstraints: and deactivateConstraints: that take an array of constraints (NSArray<NSLayoutConstraint>), so that can be helpful, especially if you have an IBOutletCollection with constraints.

[NSLayoutConstraint deactivateConstraints:myConstraints];

like image 130
Firo Avatar answered Oct 13 '22 00:10

Firo


There's no API to say "leave these views here", temporarily disabling some parts of the autolayout. There are several possible solutions:

  • remove your constraints during animation
  • arrange your constraints differently (making them to a superview of the image view, for example, or having them only depend on the center of the image view)
  • override layoutSubviews of a container view to stick your labels back where you want them after autolayout runs in [super layoutSubviews] (or remove the transform of your image view, run autolayout and then put it back)
  • subclass your image view and override frameForAlignmentRect: and alignmentRectForFrame: to get autolayout to use the untransformed frame for alignment purposes.

More details and suggestions are available in the answers here, which is a similar question of how to make transforms and autolayout play together: How do I adjust the anchor point of a CALayer, when Auto Layout is being used?

like image 41
Jesse Rusak Avatar answered Oct 13 '22 00:10

Jesse Rusak