iOS 自動レイアウト コードは NSLayout を実装します

1. まず、ビューのプロパティを設定する必要があります。translatesAutoresizingMaskIntoConstraints は NO に設定されます。自動サイズ変更と競合するため、設定されていない場合はエラーが報告されます。

 2. 制約は NSLayoutConstraint クラスであり、制約オブジェクトを作成し、[constraintWithItem: 7 パラメータ] メソッドを通じて制約オブジェクトを初期化します。

 3. 初期化時の 7 つのパラメータの説明:

        3-1:constraintWithItem:現在制約を設定しているオブジェクト。

        3-2: 属性: 現在制約されているオブジェクトによって制約される属性 (上、下、左など) NSLayoutAttributeLeft/Right/Top/Bottom...

        3-3: relationshipBy: 関連付けられている他のビューの対応する上、下、左、その他の属性を設定 (等しい/大きい/小さい) します。

        3-4: toItem: どのビューに依存するか、どのビューに依存するかを記述します。

        3-5: 属性: 従属ビュー属性 (上、下、左など) を設定します。

        3-6: multiplier: 依存オブジェクトの特定の属性の倍数である乗数を設定します。

        3-7: 定数: 依存オブジェクトの特定の属性の +- 量であるオフセットを設定します。

 4. オブジェクトに制約を追加します。

        4-1: 制約を設定するビューが他のビューに依存しない場合は、自分で制約を追加します。

        4-2: 制約を設定するビューが他のビューに依存する場合:

            4-2-1: 同じレベルのビューに依存する場合は、この制約を両方のスーパービューに追加します。

            4-2-2: 独自のスーパービューに依存する場合は、スーパービューを使用して制約を追加します。

            4-2-3: 異なるシステムおよび異なるスーパービューからのビューに依存する場合は、上方向に検索し、最も近い共通のスーパービューを使用して制約を追加します。

        4-3: ここで間違ったビューを使用して制約を追加すると、エラーが報告されますので、どのビューを使用して制約を追加するかをよく確認してください。

        4-4: 制約を追加するには 2 つの方法があります。

            4-4-1: 単一の制約を追加します [addConstraint: 制約オブジェクトを表示];

            4-4-2: 複数の制約を追加 [制約ビューの追加 addConstraint:@[制約オブジェクト, 制約オブジェクト, 制約オブジェクト...]]; 配列型

5. その他の指示:

        5-1: ステータスバーオブジェクトはself.topLayoutGuideです

        5-2: 制約を変更または変更してアニメーションが必要な場合は、アニメーション ブロックに [xxviewlayoutIfNeeded] を記述するだけです。

    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    blueView.backgroundColor = [UIColor blueColor];
    blueView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:blueView];
    
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    redView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:redView];
    
    /*
     代码实现自动布局说明:
     
     1.首先需要设置view的属性:translatesAutoresizingMaskIntoConstraints设置为NO。因为会和Autoresizing冲突,不设置会报错。
     2.约束对象为:NSLayoutConstraint,创建一个约束对象,通过[constraintWithItem:7个参数]这个方法初始化约束对象。
     3.初始化中参数说明:
        3-1:constraintWithItem:当前设置约束的对象自己。
        3-2:attribute:当前设置约束的对象自己要约束的属性(上下左右等等)NSLayoutAttributeLeft/Right/Top/Bottom...
        3-3:relatedBy:设置(相等/大于/小于)与相关联的其它view的对应上下左右等属性。
        3-4:toItem:依赖哪个view,就写哪个view。
        3-5:attribute:设置依赖view属性(上下左右等等)。
        3-6:multiplier:设置倍数,是依赖对象的某属性的多少倍。
        3-7:constant:设置偏移量,是依赖对象的某属性的+-多少。
     4.给对象添加约束:
        4-1:如果设置约束的view不依赖于其它view,那么就是自身添加此约束。
        4-2:如果是设置约束的view依赖于其它view:
            4-2-1:如果依赖同级别的view,两者的superview添加该约束。
            4-2-2:如果依赖的是自身的superview,用superview添加该约束。
            4-2-3:如果依赖的是不同系不同superview的view,向上找,用最近的共同的superview添加该约束。
        4-3:这里如果使用了错误的view添加约束,就会报错,一定小心检查确定用哪个view添加约束。
        4-4:添加约束的方法有两种:
            4-4-1:添加单个约束[添加约束的view addConstraint:约束对象];
            4-4-2:添加多个约束[添加约束的view addConstraint:@[约束对象,约束对象,约束对象...]]; 数组类型
     5.其它说明:
        5-1:状态栏对象为self.topLayoutGuide
        5-2:如果修改约束或改变约束,想要动画,只需要再动画block内写[xxview layoutIfNeeded];
     */


    //blue view布局,左侧20,上侧20,右侧20,高度50;
    NSLayoutConstraint *blueLeft = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:blueView.superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
    
    NSLayoutConstraint *blueRight = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:blueView.superview attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
    
    NSLayoutConstraint *blueTop = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:20];
    
    NSLayoutConstraint *blueHeight = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:80];
    [blueView addConstraint:blueHeight];
    [self.view addConstraints:@[blueLeft,blueRight,blueTop]];

    //redview布局,左侧中心点,上侧20,右侧20,高度同blueview
    
    //左侧leading和父view中线相同---
    NSLayoutConstraint *redLeft = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:redView.superview attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
    //依赖superview,约束加到superview
    [self.view addConstraint:redLeft];
     
    //右侧同blueview相同---依赖blueview
    NSLayoutConstraint *redRight = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
    //依赖blueView,约束加到superview
    [self.view addConstraint:redRight];
    
    //顶部和blueview间隔20---依赖blueview
    NSLayoutConstraint *redTop = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20];
    //依赖blueView,约束加到superview
    [self.view addConstraint:redTop];
    
    //高度同blueview高度---依赖blueview
    NSLayoutConstraint *redHeight = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeHeight multiplier:0.5 constant:0.0];
    //依赖blueView,约束加到superview
    [self.view addConstraint:redHeight];

おすすめ

転載: blog.csdn.net/JustinZYP/article/details/124477811