MASExampleUpdateView.m 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //
  2. // MASExampleUpdateView.m
  3. // Masonry iOS Examples
  4. //
  5. // Created by Jonas Budelmann on 3/11/13.
  6. // Copyright (c) 2013 Jonas Budelmann. All rights reserved.
  7. //
  8. #import "MASExampleUpdateView.h"
  9. @interface MASExampleUpdateView ()
  10. @property (nonatomic, strong) UIButton *growingButton;
  11. @property (nonatomic, assign) CGSize buttonSize;
  12. @end
  13. @implementation MASExampleUpdateView
  14. - (id)init {
  15. self = [super init];
  16. if (!self) return nil;
  17. self.growingButton = [UIButton buttonWithType:UIButtonTypeSystem];
  18. [self.growingButton setTitle:@"Grow Me!" forState:UIControlStateNormal];
  19. self.growingButton.layer.borderColor = UIColor.greenColor.CGColor;
  20. self.growingButton.layer.borderWidth = 3;
  21. [self.growingButton addTarget:self action:@selector(didTapGrowButton:) forControlEvents:UIControlEventTouchUpInside];
  22. [self addSubview:self.growingButton];
  23. self.buttonSize = CGSizeMake(100, 100);
  24. // make sure updateConstraints gets called
  25. [self setNeedsUpdateConstraints];
  26. return self;
  27. }
  28. // this is Apple's recommended place for adding/updating constraints
  29. - (void)updateConstraints {
  30. [self.growingButton updateConstraints:^(MASConstraintMaker *make) {
  31. make.center.equalTo(self);
  32. make.width.equalTo(@(self.buttonSize.width)).priorityLow();
  33. make.height.equalTo(@(self.buttonSize.height)).priorityLow();
  34. make.width.lessThanOrEqualTo(self);
  35. make.height.lessThanOrEqualTo(self);
  36. }];
  37. //according to apple super should be called at end of method
  38. [super updateConstraints];
  39. }
  40. - (void)didTapGrowButton:(UIButton *)button {
  41. self.buttonSize = CGSizeMake(self.buttonSize.width * 1.3, self.buttonSize.height * 1.3);
  42. // tell constraints they need updating
  43. [self setNeedsUpdateConstraints];
  44. // update constraints now so we can animate the change
  45. [self updateConstraintsIfNeeded];
  46. [UIView animateWithDuration:0.4 animations:^{
  47. [self layoutIfNeeded];
  48. }];
  49. }
  50. @end