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?
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.
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.
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 .
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.
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];
There's no API to say "leave these views here", temporarily disabling some parts of the autolayout. There are several possible solutions:
[super layoutSubviews]
(or remove the transform of your image view, run autolayout and then put it back)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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With