IOS / Autolayout:在生命周期中为代码中创建的元素设置约束

时间:2018-01-04 22:22:41

标签: ios autolayout viewdidload

我的大部分场景都在故事板中并使用自动布局。但我想在代码中创建一个UITableView,标签和视图,并将它们限制在整个自动布局中。我的问题是我应该在生命周期中创建约束。

现在,我在ViewDidLoad中创建隐藏的元素版本,然后根据数据在View Will Appear中自定义和显示它们。我不认为我可以在ViewDidLoad中放置布局约束,因为编译器不知道故事板中所有视图的位置。另一方面,每次viewWillAppear触发时,我都不想重新创建这些约束。大多数人都没有改变,最多我可能想要更新一两个。

我是否应该在viewWillAppear中放置约束并在某些测试条件下创建它们是否已经创建了它们?或者我应该将它们放在其他地方,例如viewDidlayoutSubviews或viewDidAppear?

感谢您的任何建议。

这是创建约束的代码:

  NSLayoutConstraint *contop = [NSLayoutConstraint constraintWithItem:_stepsTableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_stepNames attribute:NSLayoutAttributeBottom multiplier:1 constant:12];
     NSLayoutConstraint *contrail = [NSLayoutConstraint
     constraintWithItem:_stepsTableView attribute:NSLayoutAttributeTrailing
     relatedBy:NSLayoutRelationEqual toItem:self.scrollView
     attribute:NSLayoutAttributeTrailing multiplier:1 constant:20];
     NSLayoutConstraint *conlead = [NSLayoutConstraint
     constraintWithItem:_stepsTableView attribute:NSLayoutAttributeLeading
     relatedBy:NSLayoutRelationEqual toItem:self.scrollView
     attribute:NSLayoutAttributeLeading multiplier:1 constant:20];

     NSLayoutConstraint *conbot = [NSLayoutConstraint
     constraintWithItem:_stepsTableView attribute:NSLayoutAttributeBottom
     relatedBy:NSLayoutRelationEqual toItem:self.scrollView
     attribute:NSLayoutAttributeBottom multiplier:1 constant:20];
    NSLayoutConstraint *conheight = [NSLayoutConstraint constraintWithItem:_stepsTableView
                                                                        attribute:NSLayoutAttributeHeight
                                                                        relatedBy:NSLayoutRelationEqual
                                                                           toItem:nil
                                                                        attribute:NSLayoutAttributeNotAnAttribute
                                                                       multiplier:1.0
                                                                         constant:height];

      [self.scrollView removeConstraint: _bottomConstraint];

      [self.scrollView  addConstraints:@[contop,contrail,conlead,conbot,conheight]];

     [self.view layoutIfNeeded];

1 个答案:

答案 0 :(得分:2)

任何以编程方式创建的约束都应该放在viewDidLayoutSubviews中并使用bool值进行包装,因为在启动viewController期间多次调用该函数

   -(void)viewDidLayoutSubviews
   {
      if(once){

        once = NO;


      _stepsTableView.translatesAutoresizingMaskIntoConstraints = NO;


     NSLayoutConstraint *contop = [NSLayoutConstraint constraintWithItem:_stepsTableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_stepNames attribute:NSLayoutAttributeBottom multiplier:1 constant:12];
     NSLayoutConstraint *contrail = [NSLayoutConstraint
     constraintWithItem:_stepsTableView attribute:NSLayoutAttributeTrailing
     relatedBy:NSLayoutRelationEqual toItem:self.scrollView
     attribute:NSLayoutAttributeTrailing multiplier:1 constant:20];
     NSLayoutConstraint *conlead = [NSLayoutConstraint
     constraintWithItem:_stepsTableView attribute:NSLayoutAttributeLeading
     relatedBy:NSLayoutRelationEqual toItem:self.scrollView
     attribute:NSLayoutAttributeLeading multiplier:1 constant:20];

     NSLayoutConstraint *conbot = [NSLayoutConstraint
     constraintWithItem:_stepsTableView attribute:NSLayoutAttributeBottom
     relatedBy:NSLayoutRelationEqual toItem:self.scrollView
     attribute:NSLayoutAttributeBottom multiplier:1 constant:20];
    NSLayoutConstraint *conheight = [NSLayoutConstraint constraintWithItem:_stepsTableView
                                                                        attribute:NSLayoutAttributeHeight
                                                                        relatedBy:NSLayoutRelationEqual
                                                                           toItem:nil
                                                                        attribute:NSLayoutAttributeNotAnAttribute
                                                                       multiplier:1.0
                                                                         constant:height];

      [self.scrollView removeConstraint: _bottomConstraint];

      [self.scrollView  addConstraints:@[contop,contrail,conlead,conbot,conheight]];

     [self.view layoutIfNeeded];


      }

   }