gpt4 book ai didi

ios - 什么是 setTranslatesAutoresizingMaskIntoConstraints :NO for?

转载 作者:技术小花猫 更新时间:2023-10-29 11:14:57 26 4
gpt4 key购买 nike

在我试图理解的一些项目中有一些代码:

[[[self foregroundContentView] contentView] addSubview:[self hueLabel]];
[[[self foregroundContentView] contentView] addSubview:[self hueSlider]];

[[[self foregroundContentView] contentView] addSubview:[self saturationLabel]];
[[[self foregroundContentView] contentView] addSubview:[self saturationSlider]];

[[[self foregroundContentView] contentView] addSubview:[self brightnessLabel]];
[[[self foregroundContentView] contentView] addSubview:[self brightnessSlider]];

[[[self foregroundContentView] contentView] addSubview:[self saveButton]];


[[self foregroundContentScrollView] addSubview:[self foregroundContentView]];
[[self foregroundContentScrollView] addSubview:[self imageView]];

[[self view] addSubview:[self backgroundView]];
[[self view] addSubview:[self foregroundContentScrollView]];

每个 View 都有一个属性 setTranslatesAutoresizingMaskIntoConstraints 设置为 NO,例如:

[[self saveButton] setTranslatesAutoresizingMaskIntoConstraints:NO];

然后它为这些 View 添加了一些约束:

    NSDictionary* views = @{
@"backgroundView" : [self backgroundView],
@"foregroundContentScrollView" : [self foregroundContentScrollView],
@"foregroundContentView" : [self foregroundContentView],
@"hueLabel" : [self hueLabel],
@"hueSlider" : [self hueSlider],
@"saturationLabel" : [self saturationLabel],
@"saturationSlider" : [self saturationSlider],
@"brightnessLabel" : [self brightnessLabel],
@"brightnessSlider" : [self brightnessSlider],
@"saveButton" : [self saveButton],
@"imageView" : [self imageView]
};

[[self view] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[backgroundView]|" options:0 metrics:nil views:views]];
[[self view] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[backgroundView]|" options:0 metrics:nil views:views]];

[[self view] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[foregroundContentScrollView]|" options:0 metrics:nil views:views]];
[[self view] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[foregroundContentScrollView]|" options:0 metrics:nil views:views]];

[[self view] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[foregroundContentView]|" options:0 metrics:nil views:views]];
[[self view] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[foregroundContentView]|" options:0 metrics:nil views:views]];

[[self view] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[hueLabel]-|" options:0 metrics:nil views:views]];
[[self view] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[hueSlider]-|" options:0 metrics:nil views:views]];
[[self view] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[saturationLabel]-|" options:0 metrics:nil views:views]];
[[self view] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[saturationSlider]-|" options:0 metrics:nil views:views]];
[[self view] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[brightnessLabel]-|" options:0 metrics:nil views:views]];
[[self view] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[brightnessSlider]-|" options:0 metrics:nil views:views]];
[[self view] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[saveButton]-|" options:0 metrics:nil views:views]];
[[self view] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[imageView(==foregroundContentScrollView)]|" options:0 metrics:nil views:views]];

[[self view] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=30)-[hueLabel]-[hueSlider]-[saturationLabel]-[saturationSlider]-[brightnessLabel]-[brightnessSlider]-[saveButton]-(>=10)-[imageView(==200)]|" options:0 metrics:nil views:views]];

问题是:

  • setTranslatesAutoresizingMaskIntoConstraints:NO 有什么用?为什么当我用那种方法删除其中一根线时,一切都坏了?
  • 那行是做什么的?

    [[self view] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[foregroundContentScrollView]|" options:0 metrics:nil views:views]];

最佳答案

自动布局是一项相对较新的功能。这意味着存在三种类型的代码:

  • 不知道自动布局的代码。它使用 springs-and-struts 方法,因为它不知道其他任何事情。
  • 知道自动布局但至少在某些情况下选择使用 springs-and-struts 的代码。
  • 使用自动布局的代码。

这适用于您的代码、框架 (UIKit) 的代码以及第三方库的代码。

因为第一类代码的存在,所有 View 默认都必须具有旧的 springs-and-struts 行为。知道自动布局的代码可以根据需要将其关闭,但不知道的代码显然无法打开它,因为它不知道必须这样做或设置什么属性才能这样做。

因为旧的 springs-and-struts 行为会与大多数使用自动布局的方式发生冲突,所以使用自动布局的代码通常必须将其关闭。它通过将 translatesAutoresizingMaskIntoConstraints 属性设置为 NO 来实现。

这个决定必须留给将有问题的 View 放入 View 层次结构的 Controller 代码,因为只有那个代码知道它想如何在布局中安排 View (或者,在第一种类型的情况下代码,不“知道”但出于无知而使用 Spring 和支柱)。

如果您未能在使用自动布局的 Controller 代码中清除 translatesAutoresizingMaskIntoConstraints,您通常会由于无法满足的约束而出现异常。异常将被捕获并大部分被忽略,但自动布局系统将不得不忽略您的一些约束以完成布局。因此,您的观点可能会出现错误的布局。

这一行:

[[self view] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[foregroundContentScrollView]|" options:0 metrics:nil views:views]];

添加约束,使 foregroundContentScrollView 水平填充其 super View 。该 View 的前缘将与其父 View 的前缘相等,其后缘将与其父 View 的后缘相等。

关于ios - 什么是 setTranslatesAutoresizingMaskIntoConstraints :NO for?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28866448/

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