ViewController.m
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UISwitch *modeSwitch;
@property (weak, nonatomic) IBOutlet UIView *yellowView;
@property (weak, nonatomic) IBOutlet UIView *blueView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewSpacingContraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *blueViewConstraint;
@end
@implementation ViewController
NSString *modeUserDefaultKey = @”modeUserDefaultKey”;
– (void)viewDidLoad {
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.modeSwitch.on = [defaults boolForKey:modeUserDefaultKey];
[self updateConstraintsForMode];
}
– (IBAction)enableMode:(UISwitch *)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:sender.isOn forKey:modeUserDefaultKey];
[defaults synchronize];
[self.view layoutIfNeeded];
[UIView animateWithDuration:1.0 animations:^{
[self updateConstraintsForMode];
[self.view layoutIfNeeded];
}];
}
– (void)updateConstraintsForMode {
if (self.modeSwitch.isOn) {
self.viewSpacingContraint.constant = 8.0;
self.blueViewConstraint.priority = UILayoutPriorityDefaultHigh+1;
} else {
self.viewSpacingContraint.constant = self.view.frame.size.width;
self.blueViewConstraint.priority = UILayoutPriorityDefaultHigh–1;
}
}
@end