Skip to content

Instantly share code, notes, and snippets.

@acalism
Last active June 14, 2018 08:32
Show Gist options
  • Save acalism/d4414af29e55dbc6fc778aecfbb8bdd7 to your computer and use it in GitHub Desktop.
Save acalism/d4414af29e55dbc6fc778aecfbb8bdd7 to your computer and use it in GitHub Desktop.

Revisions

  1. acalism revised this gist Jun 14, 2018. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions UpdateViewConstraints.m
    Original file line number Diff line number Diff line change
    @@ -26,9 +26,12 @@ - (void)viewDidLoad {
    _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
    }

  2. acalism revised this gist Jun 14, 2018. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions UpdateViewConstraints.m
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    #import <Masonry/Masonry.h>


    @implementation ViewController {
    UIView * _v;
    UILabel * _label;
  3. acalism revised this gist Jun 14, 2018. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions UpdateViewConstraints.m
    Original file line number Diff line number Diff line change
    @@ -23,9 +23,10 @@ - (void)viewDidLoad {
    _label.text = @"hey";
    [_v addSubview:_label];

    // IMPORTANT: 否则updateViewConstraints不被执行
    self.view.translatesAutoresizingMaskIntoConstraints = false;
    //[self.view setNeedsUpdateConstraints];
    //[self.view setNeedsLayout];
    //[self.view setNeedsUpdateConstraints]; // another method, but it trigger -[UIViewController updateViewConstraints] executed twice
    //[self.view setNeedsLayout]; // useless code
    }

    - (void)updateViewConstraints {
  4. acalism revised this gist Jun 14, 2018. 1 changed file with 13 additions and 7 deletions.
    20 changes: 13 additions & 7 deletions UpdateViewConstraints.m
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    @implementation ViewController {
    UIView * _v;
    UILabel * _label;

    bool _constraintsAdded;
    }

    - (void)viewDidLoad {
    @@ -27,13 +29,17 @@ - (void)viewDidLoad {
    }

    - (void)updateViewConstraints {
    [_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);
    }];
    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];
  5. acalism created this gist Jun 13, 2018.
    43 changes: 43 additions & 0 deletions UpdateViewConstraints.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    @implementation ViewController {
    UIView * _v;
    UILabel * _label;
    }

    - (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];

    self.view.translatesAutoresizingMaskIntoConstraints = false;
    //[self.view setNeedsUpdateConstraints];
    //[self.view setNeedsLayout];
    }

    - (void)updateViewConstraints {
    [_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