SnapKit / Masonry

Harness the power of AutoLayout NSLayoutConstraints with a simplified, chainable and expressive syntax. Supports iOS and OSX Auto Layout
MIT License
18.05k stars 3.14k forks source link

"Grow Me" button does not work #440

Open tangzhentao opened 7 years ago

tangzhentao commented 7 years ago

Issue Info

Info Value
Platform ios
Platform Version 11
Masonry Version 1.0.2
Integration Method cocoapods

Issue Description

Masonry iOS Examples--> Update Constraints --> tap "Grow Me" button. grow button no work

It did not work.

murphy-yu commented 6 years ago

Try below:

make.width.equalTo(@(self.buttonSize.width));//.priorityLow(); make.height.equalTo(@(self.buttonSize.height));//.priorityLow();

ysylyc commented 6 years ago

@murphy-yu

if you dont mind, why is it?

cntrump commented 3 years ago
@implementation MASExampleUpdateView

- (instancetype)init {
    if (self = [super init]) {
        self.growingButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.growingButton setTitle:@"Grow Me!" forState:UIControlStateNormal];
        self.growingButton.layer.borderColor = UIColor.greenColor.CGColor;
        self.growingButton.layer.borderWidth = 3;

        [self.growingButton addTarget:self action:@selector(didTapGrowButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.growingButton];
        [self.growingButton makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.width.equalTo(@(self.buttonSize.width)).priorityLow();
            make.height.equalTo(@(self.buttonSize.height)).priorityLow();
            make.width.lessThanOrEqualTo(self);
            make.height.lessThanOrEqualTo(self);
        }];
    }

    self.buttonSize = CGSizeMake(100, 100);

    return self;
}

+ (BOOL)requiresConstraintBasedLayout
{
    return YES;
}

// this is Apple's recommended place for adding/updating constraints
- (void)updateConstraints {
    //according to apple super should be called at end of method
    [super updateConstraints];
}

- (void)didTapGrowButton:(UIButton *)button {
    self.buttonSize = CGSizeMake(self.buttonSize.width * 1.3, self.buttonSize.height * 1.3);
    [self.growingButton updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(self.buttonSize.width)).priorityLow();
        make.height.equalTo(@(self.buttonSize.height)).priorityLow();
    }];

    // tell constraints they need updating
    [self setNeedsUpdateConstraints];

    // update constraints now so we can animate the change
    [self updateConstraintsIfNeeded];

    [UIView animateWithDuration:0.4 animations:^{
        [self layoutIfNeeded];
    }];
}

@end