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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17242830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:02:12  来源:igfitidea点击:

Setting Constraints programmatically different from setting them in IB?

iosuiviewautolayout

提问by user1459524

I am new to Auto Layout in iOS. I really like the concept in principle, but it's driving me nuts trying to get the simplest things done. I suspect I'm still missing some simple underlying principle. I am trying to learn by doing and get the basics right before actually working with it in an app, so I'm creating very simple test projects. Here's one as simple as it gets that doesn't work as expected. First the part that works. In IB, I add a View to fill the entire viewcontroller, and XCode automatically sets the constraints to Top/Bottom/Leading/Trailing and the Space to 0. When done with IB, it works as expected:

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

enter image description here

在此处输入图片说明

rotates to

旋转到

enter image description here

在此处输入图片说明

Great!

伟大的!

Now I am trying to do the same thing in code:

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

- (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]];
}

That's all the code there is other than the default code for a Single-view application.

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

When I run the above code, trying to mirror the same thing as I get with IB programmatically, I get this after rotation:

当我运行上面的代码时,试图以编程方式镜像与我使用 IB 得到的相同的东西,我在旋转后得到这个:

enter image description here

在此处输入图片说明

How come that the same constraints lead to different results? It's probably something really simple and embarrassingly stupid that I am missing. Help!!!

为什么相同的约束会导致不同的结果?这可能是我遗漏的一些非常简单和令人尴尬的愚蠢事情。帮助!!!

采纳答案by geraldWilliam

Here's how I would do this:

这是我将如何做到这一点:

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[redView]|"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:NSDictionaryOfVariableBindings(redView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[redView]|"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:NSDictionaryOfVariableBindings(redView)]];

AutoLayout in IB can be a pain and I found that familiarizing myself with the Visual Format Language was less work than constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:

IB 中的 AutoLayout 可能很痛苦,我发现让自己熟悉 Visual Format Language 比 ConstraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant 要少:

回答by TommyBs

I know this is old, but I think the issue with your constraints was that you were confusing what trailing etc. represent. I've amended your code to the following and it now fills the screen as expected:

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

       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]];

You were setting the trailing constraint equal to the bottom (trailing is actually the right hand side not the bottom and you were relating the top constraint to the left hand side (leading)

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

回答by Ben Wheeler

Instead of Leading and Trailing, use Top and Bottom. Note that you are mixing these up in original post. Leading and Trailing seem to confuse people!

而不是领先和尾随,使用顶部和底部。请注意,您在原始帖子中将这些混合在一起。领先和落后似乎混淆了人们!