Skip to content

Instantly share code, notes, and snippets.

@acalism
Last active June 14, 2018 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acalism/d4414af29e55dbc6fc778aecfbb8bdd7 to your computer and use it in GitHub Desktop.
Save acalism/d4414af29e55dbc6fc778aecfbb8bdd7 to your computer and use it in GitHub Desktop.
#import <Masonry/Masonry.h>
@implementation ViewController {
UIView * _v;
UILabel * _label;
bool _constraintsAdded;
}
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *l = [UILabel new];
l.text = @"我能优先显示吗?";
l.tintColor = [UIColor magentaColor];
l.frame = CGRectMake(100, 50, 160, 20);
[self.view addSubview:l];
_v = UIView.new;
_v.backgroundColor = [UIColor greenColor];
[self.view addSubview:_v];
_label = UILabel.new;
_label.tintColor = [UIColor redColor];
_label.text = @"hey";
[_v addSubview:_label];
// Do NOT add any constraint at here, or else `updateViewController` will be called twice.
// IMPORTANT: 否则updateViewConstraints不被执行
self.view.translatesAutoresizingMaskIntoConstraints = false;
//[self.view setNeedsUpdateConstraints]; // another method, but it trigger -[UIViewController updateViewConstraints] executed twice
//[self.view setNeedsLayout]; // useless code
}
- (void)updateViewConstraints {
if (!_constraintsAdded) {
_constraintsAdded = true;
[_v mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(100, 50, 200, 30));
}];
[_label mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(_v);
}];
}
NSLog(@"update constraint.");
[super updateViewConstraints];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment