Ios 如何使用自动布局使b/w UIFields具有相等的空间?

Ios 如何使用自动布局使b/w UIFields具有相等的空间?,ios,objective-c,autolayout,nslayoutconstraint,Ios,Objective C,Autolayout,Nslayoutconstraint,我是一个iOS自动布局的初学者,我正在视图控制器上添加五个标签,所以每件事都可以。(此处五个标签的宽度和高度是恒定的。) 我的主要要求是如何使五个标签具有相等的水平间距。我可以设置中间标签和左右角标签,它们是完美的。但我不知道如何添加第二个左标签和第二个右标签,以及如何像其他标签一样使它们具有相等的空间 我的要求如下图所示,请帮助我 我的代码: #import "ViewController2.h" @interface ViewController2 () { UILabel * l

我是一个iOS自动布局的初学者,我正在视图控制器上添加五个标签,所以每件事都可以。(此处五个标签的宽度和高度是恒定的。)

我的主要要求是如何使五个标签具有相等的水平间距。我可以设置中间标签和左右角标签,它们是完美的。但我不知道如何添加第二个左标签和第二个右标签,以及如何像其他标签一样使它们具有相等的空间

我的要求如下图所示,请帮助我

我的代码:

#import "ViewController2.h"

@interface ViewController2 ()
{
    UILabel * left1;
    UILabel * left2;
    UILabel * middle;
    UILabel * right1;
    UILabel * right2;
}

@end

@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];

    left1 = [[UILabel alloc] init];
    left1.backgroundColor = [UIColor grayColor];
    left1.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:left1];

    left2 = [[UILabel alloc] init];
    left2.backgroundColor = [UIColor grayColor];
    left2.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:left2];

    middle = [[UILabel alloc] init];
    middle.backgroundColor = [UIColor grayColor];
    middle.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:middle];

    right1 = [[UILabel alloc] init];
    right1.backgroundColor = [UIColor grayColor];
    right1.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:right1];

    right2 = [[UILabel alloc] init];
    right2.backgroundColor = [UIColor grayColor];
    right2.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:right2];


    //Applying autolayouts for middle lable

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:middle
                                                            attribute:NSLayoutAttributeTop
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:self.view
                                                            attribute:NSLayoutAttributeTop
                                                           multiplier:1.0
                                                             constant:100]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:middle
                                                            attribute:NSLayoutAttributeCenterX
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:self.view
                                                            attribute:NSLayoutAttributeCenterX
                                                           multiplier:1.0
                                                             constant:10]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:middle
                                                            attribute:NSLayoutAttributeHeight
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:nil
                                                            attribute:0
                                                           multiplier:1.0
                                                             constant:50]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:middle
                                                            attribute:NSLayoutAttributeWidth
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:nil
                                                            attribute:0
                                                           multiplier:1.0
                                                             constant:20]];

    //Appying autolayouts for left1 labe1

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:left1
                                                          attribute:NSLayoutAttributeTop
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeTop
                                                         multiplier:1.0
                                                           constant:100]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:left1
                                                          attribute:NSLayoutAttributeLeading
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeLeading
                                                         multiplier:1.0
                                                           constant:10]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:left1
                                                          attribute:NSLayoutAttributeHeight
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:nil
                                                          attribute:0
                                                         multiplier:1.0
                                                           constant:50]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:left1
                                                          attribute:NSLayoutAttributeWidth
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:nil
                                                          attribute:0
                                                         multiplier:1.0
                                                           constant:20]];

    //Appying autolayouts for right1 labe1

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:right1
                                                          attribute:NSLayoutAttributeTop
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeTop
                                                         multiplier:1.0
                                                           constant:100]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:right1
                                                          attribute:NSLayoutAttributeTrailing
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeTrailing
                                                         multiplier:1.0
                                                           constant:-10]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:right1
                                                          attribute:NSLayoutAttributeHeight
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:nil
                                                          attribute:0
                                                         multiplier:1.0
                                                           constant:50]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:right1
                                                          attribute:NSLayoutAttributeWidth
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:nil
                                                          attribute:0
                                                         multiplier:1.0
                                                           constant:20]];


}

@end
以下是您的步骤(通过IB)

  • 创建5个标签
  • 设置左侧和右侧标签约束(用于左前导、顶部、宽度、高度;用于右尾随、顶部、宽度和高度)
  • 中心水平中间标签(顶部约束、宽度、高度、中心水平)
  • 这里有一些技巧-在左标签和中标签+中标签和右标签之间添加两个容器视图
  • 此容器的设置约束(前导和尾随约束+顶部+高度)此容器将根据屏幕大小灵活设置
  • 添加约束后,它应该是这样的

  • 最后一步-将其他标签放置到绿色容器中,并设置中间标签的约束(它应位于容器的中心+添加顶部、宽度和高度约束)
  • 所有约束都以左边的大小提供,因此如果需要,您可以通过代码轻松地重新创建它


    希望这有帮助,所以我知道你的问题已经得到了回答,但是在iOS9中有一些视图是专门为这种情况构建的。以下是如何使用它们以供将来参考:

  • 按住Shift键并单击并拖动以选择所有标签(注意,不必调整标签的大小)

  • 单击屏幕右下角的StackView按钮

  • 然后选择StackView并将对齐设置为“填充”,将分布设置为“等间距”

  • 然后,在stackview仍处于选中状态的情况下,单击pin图标,将200放置在顶部,10和10放置在侧面,130放置在高度。然后单击添加4个约束。

  • 最后单击三角形图标并选择“更新帧”。


  • 瞧!你有你的布局,而不必使用间隔

    我想你想要什么可以通过谷歌找到:通过代码解释我不幸的是,我们仍然需要支持旧版本:)好的解决方案