NSLayoutConstraint - 系统自动布局

主要还是使用 Masonry ,看另一篇文章,这里大致了解系统自动布局的方法,其实masonry 与系统的差不多。只是简化了代码量。

一般方法 添加约束

  • 1 首先 需要对自动布局的 obj 设置:
    self.leftView.translatesAutoresizingMaskIntoConstraints = NO;
  • 2 添加约束,简单介绍,需要注意的挺多,不小心就会报错哦。
// 父视图 添加约束 :方程式:
leftView 的 top = self.view 的 top (倍数1) + 100;

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.leftView
                                                         attribute:NSLayoutAttributeTop
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self.view
                                                         attribute:NSLayoutAttributeTop
                                                        multiplier:1.
                                                           constant:100]];
  • 3 对于 约束 存在优先级
    NSLayoutConstraint *leftTop = [NSLayoutConstraint constraintWithItem:self.leftView
                                                               attribute:NSLayoutAttributeTop
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.view
                                                               attribute:NSLayoutAttributeTop
                                                              multiplier:1.
                                                                constant:100];
    leftTop.priority = UILayoutPriorityDefaultHigh;
  • 3 举个例子:左右两个view 等高等宽,自动布局
    self.leftView = [UIView new];
    self.leftView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.leftView];
    self.rightView = [UIView new];
    self.rightView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.rightView];

    self.leftView.translatesAutoresizingMaskIntoConstraints = NO;
    self.rightView.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.leftView
                                                         attribute:NSLayoutAttributeTop
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self.view
                                                         attribute:NSLayoutAttributeTop
                                                        multiplier:1.
                                                           constant:100]];
    // l 左 = s 左 + 20
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.leftView
                                                         attribute:NSLayoutAttributeLeft
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self.view
                                                         attribute:NSLayoutAttributeLeft
                                                        multiplier:1.
                                                           constant:20]];
    
    // l 高 = 200;
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.leftView
                                                         attribute:NSLayoutAttributeHeight
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:nil
                                                         attribute:NSLayoutAttributeHeight
                                                        multiplier:1.
                                                           constant:200]];
    
    // r 上 = l 上
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.rightView
                                                         attribute:NSLayoutAttributeTop
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self.leftView
                                                         attribute:NSLayoutAttributeTop
                                                        multiplier:1.
                                                           constant:0]];
    
    // r 左 = l 右 + 20
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.rightView
                                                         attribute:NSLayoutAttributeLeft
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self.leftView
                                                         attribute:NSLayoutAttributeRight
                                                        multiplier:1.
                                                           constant:20]];
    
    // r 右 = s 右 - 20
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.rightView
                                                         attribute:NSLayoutAttributeRight
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self.view
                                                         attribute:NSLayoutAttributeRight
                                                        multiplier:1.
                                                           constant:-20]];
    
    // r 高 = l 高
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.rightView
                                                         attribute:NSLayoutAttributeHeight
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self.leftView
                                                         attribute:NSLayoutAttributeHeight
                                                        multiplier:1.
                                                           constant:0]];
    
    // r 宽 = l 宽
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.rightView
                                                          attribute:NSLayoutAttributeWidth
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.leftView
                                                          attribute:NSLayoutAttributeWidth
                                                         multiplier:1.0
                                                           constant:0]];

format 方式 添加约束

略坑 大概了解一点点
注意 obj 需要 [obj] 框起来,其他好像没什么要注意的,用起来没有上面的方便,主要是不熟练。


    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[leftView(==200)]" options:0 metrics:nil views:@{@"leftView":self.leftView,@"rightView":self.rightView}]];
    
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[rightView(==200)]" options:0 metrics:nil views:@{@"leftView":self.leftView,@"rightView":self.rightView}]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[leftView]-10-[rightView(==leftView)]-20-|" options:0 metrics:nil views:@{@"leftView":self.leftView,@"rightView":self.rightView}]];

1

你可能感兴趣的:(NSLayoutConstraint - 系统自动布局)