day9約束---layout

  • (void)viewDidLoad {
    [super viewDidLoad];

    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor redColor];

    //用Constraints(layout)來布局;哪個(gè)視圖(view)使用layout布局哪個(gè)控件就必須寫這句話
    [view setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addSubview:view];

    //****************** 給視圖添加約束的四個(gè)步驟 ***************************
    //被添加的約束對(duì)象的superView
    //第一個(gè)參數(shù):表示被添加約束的對(duì)象;
    //第二個(gè)參數(shù):表示被約束的方向;是一個(gè)枚舉值;
    /*
    NSLayoutAttributeLeft = 1,
    NSLayoutAttributeRight,
    NSLayoutAttributeTop,
    NSLayoutAttributeBottom,
    NSLayoutAttributeLeading,
    NSLayoutAttributeTrailing,
    NSLayoutAttributeWidth,
    NSLayoutAttributeHeight,
    NSLayoutAttributeCenterX,
    NSLayoutAttributeCenterY,
    */
    //第三個(gè)參數(shù): 寫零

    //第四個(gè)參數(shù):參照的對(duì)象
    //第五個(gè)參數(shù):與參照對(duì)象的方向
    //第六個(gè)參數(shù):比例,指的是view的寬和高的比例
    //第七個(gè)參數(shù):兩個(gè)視圖的距離
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeft relatedBy:0 toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:30]];

//確定y軸
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:0 toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:30]];

//******* 第一種約束方法 ********
//單獨(dú)給約束對(duì)象寬度,是不需要給參照對(duì)象的;
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:40]];

//******* 第二種約束方法 *******
//用一個(gè)view的寬度來約束另一個(gè)view的寬度;約束是相互制約的
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:0 toItem:view attribute:NSLayoutAttributeHeight multiplier:1 constant:0]];



UIView *view1 = [[UIView alloc] init];
view1.backgroundColor = [UIColor blueColor];;
[view1 setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:view1];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft relatedBy:0 toItem:view attribute:NSLayoutAttributeRight multiplier:1 constant:40]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:0 toItem:view attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:0 toItem:view attribute:NSLayoutAttributeHeight multiplier:1 constant:0]];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容