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

Posted

技术标签:

【中文标题】如何以编程方式使用自动布局以编程方式添加 UIview?【英文标题】:How to add UIviews programmatically using auto-layouts parogramatically? 【发布时间】:2015-09-04 06:34:13 【问题描述】:

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

这里我的主要要求是我想将第一个视图(即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)]];


【问题讨论】:

你可以试试包装器github.com/robb/Cartography Creating layout constraints programmatically的可能重复 【参考方案1】:

截图

以及使用 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]];

【讨论】:

是的,你的权利,但我希望这个答案与项目甲酸盐相一致 @Krish 看,我更新了什么。我建议使用可视化格式语言。它简短明了

以上是关于如何以编程方式使用自动布局以编程方式添加 UIview?的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式添加标签并以编程方式使用自动布局定位它

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

如何使用自动布局在 ios 中以编程方式在 UIview 上添加 UIbutton

如何以编程方式在 UICollectionView 上添加自动布局?

使用自动布局以编程方式添加背景图像视图

以编程方式布局 UIScrollView,并为其子视图添加了自动布局,但它不滚动