【问题标题】:Setting Constraints programmatically different from setting them in IB?以编程方式设置约束与在 IB 中设置约束不同?
【发布时间】:2013-06-19 00:47:21
【问题描述】:

我是 iOS 中自动布局的新手。原则上我真的很喜欢这个概念,但它让我疯狂地试图完成最简单的事情。我怀疑我仍然缺少一些简单的基本原则。我正在尝试边做边学,并在实际使用应用程序之前获得基础知识,因此我正在创建非常简单的测试项目。这是一个尽可能简单但无法按预期工作的方法。首先是有效的部分。在 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]];
}

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

当我运行上述代码时,尝试以编程方式反映与使用 IB 相同的内容时,我在轮换后得到了这个:

为什么相同的约束会导致不同的结果?我想念的可能是一些非常简单且令人尴尬的愚蠢。救命!!!

【问题讨论】:

    标签: ios uiview autolayout


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      使用顶部和底部代替前导和尾随。请注意,您在原始帖子中将这些混合在一起。领先和尾随似乎让人感到困惑!

      【讨论】:

        【解决方案3】:

        我会这样做:

        [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 中的 AutoLayout 可能会很痛苦,我发现熟悉视觉格式语言比 constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant: 工作要少。

        【讨论】:

        • 谢谢!这行得通(在 V:|[redView]...
        • 是的,我在那里少了一根管子。固定的。而且,老实说,我不知道。我找到了 VFL,并没有进一步满足我的 AutoLayout 需求。
        • 大家好。 6 个月后,我遇到了同样的问题,但我想直接使用 mycontroller.view,而不是 mycontroller.view 中的视图。它只是做......什么都没有!
        • @shinyuX 我知道这是旧的(希望你已经解决了这个问题),为了使用代码中的这些约束,你必须将translatesAutoresizingMaskIntoConstraints 设置为NO
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-26
        • 1970-01-01
        相关资源
        最近更新 更多