如何以编程方式使用自动布局以编程方式添加UI视图?

时间:2015-09-04 06:34:13

标签: ios objective-c autolayout

您好我正在尝试使用自动布局以编程方式添加UIViews

这里我的主要要求是我要将firstView(即myView1)尾随空格设置为“30”,并且我想将第二个视图(即myView2)设置为距离第一个30查看下面的图像。

为此,我编写了一些代码(使用视觉格式和带有项目格式的约束),但这不起作用。

代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UIView *myView1 = [[UIView alloc] init];
    myView1.backgroundColor = [UIColor redColor];
    myView1.translatesAutoresizingMaskIntoConstraints = NO;  
    [self.view addSubview:myView1];

    UIView *myView2 = [[UIView alloc] init];
    myView2.backgroundColor = [UIColor orangeColor];
    myView2.translatesAutoresizingMaskIntoConstraints = NO;  
    [self.view addSubview:myView2];


    [self.view addConstraints:[NSLayoutConstraint
                               constraintsWithVisualFormat:@"V:|-[myView1(==50)]-50-|"
                               options:0
                               metrics:nil
                               views:NSDictionaryOfVariableBindings(myView1)]];

    [self.view addConstraints:[NSLayoutConstraint
                               constraintsWithVisualFormat:@"V:|-[myView2(==50)]-50-|"
                               options:0
                               metrics:nil
                               views:NSDictionaryOfVariableBindings(myView2)]];

//Constraints with item formate
            NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:myView1 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-30.f];
            [self.view addConstraint:constraint];

            constraint = [NSLayoutConstraint constraintWithItem:myView1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:50.0f];
            [self.view addConstraint:constraint];

           NSLayoutConstraint*constraint1 = [NSLayoutConstraint constraintWithItem:myView2 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:myView1 attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-30.f];
          [self.view addConstraint:constraint1];

           constraint1 = [NSLayoutConstraint constraintWithItem:myView2 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:50.0f];
          [self.view addConstraint:constraint1];


//Constraints with visual formate
  [self.view addConstraints:[NSLayoutConstraint
                               constraintsWithVisualFormat:@"H:|-[myView1(==50)]-20-[myView(==50)]-30-|"
                               options:0
                               metrics:nil
                               views:NSDictionaryOfVariableBindings(myView1,myView2)]];

}

enter image description here

1 个答案:

答案 0 :(得分:3)

截图

使用Visual Formatter语言编写代码

UIView *myView1 = [[UIView alloc] init];
myView1.backgroundColor = [UIColor redColor];
myView1.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:myView1];

UIView *myView2 = [[UIView alloc] init];
myView2.backgroundColor = [UIColor orangeColor];
myView2.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:myView2];

NSDictionary * viewsDic = NSDictionaryOfVariableBindings(myView1,myView2);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[myView1(50)]-30-[myView2(50)]-30-|"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:viewsDic]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[myView1(50)]-30-|"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:viewsDic]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[myView2(50)]-30-|"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:viewsDic]];

更新,以项目格式编码

UIView *myView1 = [[UIView alloc] init];
myView1.backgroundColor = [UIColor redColor];
myView1.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:myView1];

UIView *myView2 = [[UIView alloc] init];
myView2.backgroundColor = [UIColor orangeColor];
myView2.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:myView2];

//View2.width=50
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:myView2
                                                      attribute:NSLayoutAttributeWidth
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:nil
                                                      attribute:0
                                                     multiplier:1.0
                                                       constant:50]];
//View2.height=50
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:myView2
                                                      attribute:NSLayoutAttributeHeight
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:nil
                                                      attribute:0
                                                     multiplier:1.0
                                                       constant:50]];
//View2.traling = self.traling - 30
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:myView2
                                                      attribute:NSLayoutAttributeTrailing
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeTrailing
                                                     multiplier:1.0
                                                       constant:-30]];

//View2.bottom = self.bottom - 30
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:myView2
                                                      attribute:NSLayoutAttributeBottom
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeBottom
                                                     multiplier:1.0
                                                       constant:-30]];

//View1.width = 50
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:myView1
                                                      attribute:NSLayoutAttributeWidth
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:nil
                                                      attribute:0
                                                     multiplier:1.0
                                                       constant:50]];
//View1.height=50
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:myView1
                                                      attribute:NSLayoutAttributeHeight
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:nil
                                                      attribute:0
                                                     multiplier:1.0
                                                       constant:50]];

//View1.traling = View2.leading - 30
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:myView1
                                                      attribute:NSLayoutAttributeTrailing
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:myView2
                                                      attribute:NSLayoutAttributeLeading
                                                     multiplier:1.0
                                                       constant:-30]];
//View1.bottom = self.bottom - 30
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:myView1
                                                      attribute:NSLayoutAttributeBottom
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeBottom
                                                     multiplier:1.0
                                                       constant:-30]];