Last active
June 14, 2018 08:32
-
-
Save acalism/d4414af29e55dbc6fc778aecfbb8bdd7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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
Reference: https://stackoverflow.com/questions/17497002/when-will-or-wont-updateviewconstraints-be-called-on-my-view-controller-for-m