MASExampleUpdateView.m 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. return self;
  25. }
  26. + (BOOL)requiresConstraintBasedLayout
  27. {
  28. return YES;
  29. }
  30. // this is Apple's recommended place for adding/updating constraints
  31. - (void)updateConstraints {
  32. [self.growingButton updateConstraints:^(MASConstraintMaker *make) {
  33. make.center.equalTo(self);
  34. make.width.equalTo(@(self.buttonSize.width)).priorityLow();
  35. make.height.equalTo(@(self.buttonSize.height)).priorityLow();
  36. make.width.lessThanOrEqualTo(self);
  37. make.height.lessThanOrEqualTo(self);
  38. }];
  39. //according to apple super should be called at end of method
  40. [super updateConstraints];
  41. }
  42. - (void)didTapGrowButton:(UIButton *)button {
  43. self.buttonSize = CGSizeMake(self.buttonSize.width * 1.3, self.buttonSize.height * 1.3);
  44. // tell constraints they need updating
  45. [self setNeedsUpdateConstraints];
  46. // update constraints now so we can animate the change
  47. [self updateConstraintsIfNeeded];
  48. [UIView animateWithDuration:0.4 animations:^{
  49. [self layoutIfNeeded];
  50. }];
  51. }
  52. @end