Masonry icon indicating copy to clipboard operation
Masonry copied to clipboard

困扰已久的问题,求指点

Open kaqise opened this issue 6 years ago • 1 comments

#import "Control.h" #import <Masonry.h>

@implementation Control{ UIView *topVIew; UILabel *label; UIView *bottomView; } [topVIew mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.right.mas_equalTo(0); make.height.mas_equalTo(100); }];

[bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.bottom.right.mas_equalTo(0);
    make.height.mas_equalTo(100);
}];

[label mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.mas_equalTo(topVIew.mas_bottom);
    make.bottom.mas_equalTo(bottomView.mas_top);
    make.left.right.mas_equalTo(0);
}];

ViewController.m

  • (void)viewDidLoad { [super viewDidLoad];

    _control = [[Control alloc]init]; _control.backgroundColor = [UIColor redColor]; [self.view addSubview:_control]; [_control mas_makeConstraints:^(MASConstraintMaker *make) { make.center.mas_equalTo(self.view); make.width.mas_equalTo(self.view).multipliedBy(0.8); make.height.mas_equalTo(300); }];

}

  • (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { _result = !_result; [self layoutControl]; }

  • (void)layoutControl { [_control mas_updateConstraints:^(MASConstraintMaker *make) { if (_result) { make.height.mas_equalTo(350); }else { make.height.mas_equalTo(300); } }]; [UIView animateWithDuration:0.25 animations:^{ [self.view layoutIfNeeded]; }];

}

这样修改的时候视图高度修改为什么会跳动?

Uploading 屏幕快照 2018-07-26 上午11.27.51.png…

kaqise avatar Jul 26 '18 03:07 kaqise

在执行动画之前告诉系统你要更新约束,即在

[UIView animateWithDuration:0.25 animations:^{
[self.view layoutIfNeeded];
}];

之前执行:[self.view updateConstraintsIfNeeded];

zcgong avatar Oct 10 '18 08:10 zcgong