The new NSLayoutConstraint activateConstraints: and deactivateConstraints: methods do not work correctly with the restrictions created by IB (they work correctly for the restrictions created by the code). I created a simple one-button test application that has two sets of limitations. One installed set has restrictions on the center and center, and another set that is deleted has upper and left restrictions (constant 10). The button method toggles these sets of constraints. Here is the code
@interface ViewController () @property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *uninstalledConstraints; @property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *installedConstraints; @end @implementation ViewController - (IBAction)switchconstraints:(UIButton *)sender { [NSLayoutConstraint deactivateConstraints:self.installedConstraints]; [NSLayoutConstraint activateConstraints:self.uninstalledConstraints]; } -(void)viewWillLayoutSubviews { NSLog(@"installed: %@ uninstalled: %@", ((NSLayoutConstraint *)self.installedConstraints[0]).active ? @"Active" : @"Inactive", ((NSLayoutConstraint *)self.uninstalledConstraints[0]).active ? @"Active" : @"Inactive"); }
When the application starts, the button is in the correct, centered position, determined by the established restrictions. After I activate / inactivate in the button's action method, the button will return to its new position correctly, but when I rotate the view to landscape, it will return to its originally defined position (although the newly activated set still appears in the log as being active). When I turn back to the portrait, the button remains in its original position (in the center of the screen), and now the log shows that the initial set of restrictions is active and those that I activated are inactive.
The question is, is this a mistake or should these methods not work this way with certain IB restrictions?
ios cocoa-touch autolayout nslayoutconstraint
rdelmar Dec 27 '14 at 0:14 2014-12-27 00:14
source share