gpt4 book ai didi

ios - 以编程方式设置约束与在 IB 中设置约束不同?

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

我是 iOS 自动布局的新手。原则上我真的很喜欢这个概念,但它让我疯狂地试图完成最简单的事情。我怀疑我仍然缺少一些简单的基本原则。我正在尝试边做边学,并在实际使用应用程序之前掌握基础知识,因此我正在创建非常简单的测试项目。这是一个非常简单但无法按预期工作的方法。首先是有效的部分。在 IB 中,我添加了一个 View 来填充整个 View Controller ,XCode 自动将约束设置为 Top/Bottom/Leading/Trailing 并将 Space 设置为 0。完成 IB 后,它按预期工作:

enter image description here

旋转到

enter image description here

太棒了!

现在我正尝试在代码中做同样的事情:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView *redView = [[UIView alloc] initWithFrame:self.view.bounds];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];

redView.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f]];
}

这就是除单 View 应用程序的默认代码之外的所有代码。

当我运行上面的代码时,试图以编程方式反射(reflect)我通过 IB 获得的相同内容,我在旋转后得到了这个:

enter image description here

为什么相同的约束会导致不同的结果?这可能是我所缺少的非常简单且令人尴尬的愚蠢行为。帮忙!!!

最佳答案

我知道这已经过时了,但我认为您的约束的问题在于您混淆了尾随等代表的内容。我已将您的代码修改为以下内容,它现在按预期填满了屏幕:

       UIView *redView = [[UIView alloc] initWithFrame:self.view.bounds];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];

redView.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f]];

您将尾部约束设置为底部(尾部实际上是右侧而不是底部,并且您将顶部约束与左侧(前导)相关联

关于ios - 以编程方式设置约束与在 IB 中设置约束不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17242830/

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