gpt4 book ai didi

ios - 自动布局导致无限 layoutSubviews 循环

转载 作者:可可西里 更新时间:2023-11-01 06:18:04 28 4
gpt4 key购买 nike

我真的很困惑,当我在 subview 上使用自动布局时,layoutSubview 方法会无限循环。

我所做的是:

- (id)init
{
self = [super init];
if(self)
{
self.backgroundColor = [UIColor grayColor];
_imageView = [[UIImageView alloc] init];
_imageView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:_imageView];

[self applyConstraints];

}
return self;
}

-(void)applyConstraints
{
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_imageView]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_imageView]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView)]];
}

这会导致 layoutSubviews 中的无限循环。实际上,即使未调用 applyConstraints 也会发生循环,阻止它发生的唯一方法是将“translatesAutoresizingMaskIntoConstraints”设置为 YES。

有没有人遇到/解决过这个问题?

更新澄清一下,这是一个 UIView 子类,在 View Controller 中使用。 View 本身不使用自动布局,而是在初始化后以老式方式设置它的框架。

最佳答案

这可能对你有帮助。

layoutSubviews 将被调用

  • 当 View 的框架发生变化时
  • 当一个 View 从父 View 中移除时,它将在父 View 上被调用
  • 在 UIScrollview 中,在滚动期间。
  • 在将 View 添加到 superView 时。
  • 方向改变期间。

关于ios - 自动布局导致无限 layoutSubviews 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18609054/

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