Masonry布局二

2016-12-05 10_02_23.gif

更新約束。

//
//  ViewController.m
//  MasonrySecond
//
//  Created by mibo02 on 16/12/5.
//  Copyright ? 2016年 mibo02. All rights reserved.
//

#import "ViewController.h"
#import "Masonry.h"
@interface ViewController ()

@property (nonatomic, strong)UIButton *growingButton;
@property (nonatomic, assign)CGFloat scacle;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.growingButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.growingButton addTarget:self action:@selector(clickBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.growingButton setTitle:@"點擊放大" forState:(UIControlStateNormal)];
    self.growingButton.layer.borderColor = [UIColor greenColor].CGColor;
    self.growingButton.layer.borderWidth = 3;
    [self.view addSubview:self.growingButton];
//    [self.growingButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.center.mas_equalTo(self.view);
//        //優先級最低
//        make.width.height.mas_equalTo(100*self.scacle).priorityLow();
//        //放大到整個view上
//        make.width.height.lessThanOrEqualTo(self.view);
//    }];
    
}
//限制
- (void)updateViewConstraints
{
    [self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.view);
        //
        make.width.height.mas_equalTo(100 * self.scacle).priorityLow();
        //
        make.width.height.lessThanOrEqualTo(self.view);
    }];
    [super updateViewConstraints];
}
- (void)clickBtnAction:(UIButton *)sender
{
    self.scacle += 0.5;
    //告訴self.view約束需要更新
    [self.view setNeedsUpdateConstraints];
    //告訴view需要更新的時候在進行更新
    [self.view updateConstraintsIfNeeded];
    [UIView animateWithDuration:0.5 animations:^{
        [self.view layoutIfNeeded];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容