2013-06-21 52 views
9

我是新来的iOS自动布局。原则上我非常喜欢这个概念,但是这让我坚持要完成最简单的事情。我怀疑我仍然错过了一些简单的基本原则。我正在尝试通过实际操作学习,并在实际使用应用程序之前获得基础知识,因此我正在创建非常简单的测试项目。这是一个非常简单的方法,不会像预期的那样工作。首先是工作的部分。在IB,我添加了一个视图以填充整个视图 - 控制和XCode的自动设置约束顶部/底部/上升/下降和空间为0。当IB完成的,它按预期工作:以编程方式设置约束与在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]]; 
} 

这是所有的代码中有比单视图应用程序的默认代码等。

当我运行上面的代码,试图反映同样的事情,因为我与IB得到编程,我得到这个旋转后:

enter image description here

怎么说,同样的限制导致不同的结果?这可能是我错过的非常简单和令人尴尬的愚蠢行为。帮帮我!!!

回答

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

自动版式在IB可以是一个痛苦,我发现,熟悉自己与Visual格式语言比constraintWithItem工作量少:属性:relatedBy:toItem:属性:乘数:常数:

+0

谢谢!这工作(在添加失踪|在V:| [redView] ...但为什么这个heck不会我的其他代码工作?它是等效的到什么IB显示,不是吗??? – user1459524

+0

是的,我错过了一个管道。修正。而且,说实话,我不知道。我发现VFL,并没有进一步寻找我的AutoLayout需要。 – geraldWilliam

+0

嗨! 6个月后,我遇到了同样的问题,但我想做t直接使用mycontroller.view的帽子,而不是mycontroller.view中的视图。它只是......没有! – shinyuX

1

而不是引导和尾随,使用顶部和底部。请注意,您正在原始帖子中混合这些内容。领导和追踪似乎混淆了人们!

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

你被设置尾随约束等于底部(尾随实际上是右手边不是底部和你有关顶部约束到左侧(领先)

相关问题