gpt4 book ai didi

IOS/自动布局 : When in lifecycle to set constraints for elements created in code

转载 作者:行者123 更新时间:2023-11-28 19:29:00 24 4
gpt4 key购买 nike

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

现在,我在 ViewDidLoad 中创建元素的隐藏版本,然后根据数据在 View Will Appear 中自定义和显示它们。我不认为我可以将布局约束放在 ViewDidLoad 中,因为编译器不知道 Storyboard 中的所有 View 位于何处。另一方面,我不想在每次 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];

最佳答案

任何以编程方式创建的约束都应该放在 viewDidLayoutSubviews 中,并用 once 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];


}

}

关于IOS/自动布局 : When in lifecycle to set constraints for elements created in code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48104444/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com