以编程方式设置约束不同于在IB中设置约束?

use*_*524 9 uiview ios autolayout

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

在此输入图像描述

旋转到

在此输入图像描述

大!

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

- (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]];
}
Run Code Online (Sandbox Code Playgroud)

这是除了单视图应用程序的默认代码之外的所有代码.

当我运行上面的代码时,尝试以编程方式镜像我用IB获得的相同内容,我在轮换后得到这个:

在此输入图像描述

为什么相同的约束会导致不同的结果呢?这可能是一件非常简单而又令人尴尬的事情.救命!!!

Tom*_*yBs 7

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

       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]];
Run Code Online (Sandbox Code Playgroud)

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


ger*_*iam 6

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

[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)]];
Run Code Online (Sandbox Code Playgroud)

IB中的AutoLayout可能很麻烦,我发现熟悉Visual Format Language比使用constraintWithItem更少:attribute:relatedBy:toItem:attribute:multiplier:constant: