我是新来的iOS自动布局。原则上我非常喜欢这个概念,但是这让我坚持要完成最简单的事情。我怀疑我仍然错过了一些简单的基本原则。我正在尝试通过实际操作学习,并在实际使用应用程序之前获得基础知识,因此我正在创建非常简单的测试项目。这是一个非常简单的方法,不会像预期的那样工作。首先是工作的部分。在IB,我添加了一个视图以填充整个视图 - 控制和XCode的自动设置约束顶部/底部/上升/下降和空间为0。当IB完成的,它按预期工作:以编程方式设置约束与在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]];
}
这是所有的代码中有比单视图应用程序的默认代码等。
当我运行上面的代码,试图反映同样的事情,因为我与IB得到编程,我得到这个旋转后:
怎么说,同样的限制导致不同的结果?这可能是我错过的非常简单和令人尴尬的愚蠢行为。帮帮我!!!
谢谢!这工作(在添加失踪|在V:| [redView] ...但为什么这个heck不会我的其他代码工作?它是等效的到什么IB显示,不是吗??? – user1459524
是的,我错过了一个管道。修正。而且,说实话,我不知道。我发现VFL,并没有进一步寻找我的AutoLayout需要。 – geraldWilliam
嗨! 6个月后,我遇到了同样的问题,但我想做t直接使用mycontroller.view的帽子,而不是mycontroller.view中的视图。它只是......没有! – shinyuX