自動布局NSLayoutConstraint

自動布局方法1

/**
 創(chuàng)建約束
 
 @param view1 被約束視圖view1
 @param attr1 被約束視圖的布局屬性
 @param relation 關系屬性
 @param view2 參照視圖view2
 @param attr2 參照視圖view2的布局屬性
 @param multiplier 倍數
 @param c 常數
 @return 約束
 */
+(instancetype)constraintWithItem:(id)view1
                        attribute:(NSLayoutAttribute)attr1
                        relatedBy:(NSLayoutRelation)relation
                           toItem:(nullable id)view2
                        attribute:(NSLayoutAttribute)attr2
                       multiplier:(CGFloat)multiplier
                         constant:(CGFloat)c;

依據的公式是:view1.attr1 = view2.attr2 * multiplier + constant

NSLayoutAttribute(布局屬性)

NSLayoutAttribute的類型 含義
NSLayoutAttributeLeft 視圖的左邊
NSLayoutAttributeRight 視圖的右邊
NSLayoutAttributeTop 視圖的上邊
NSLayoutAttributeBottom 視圖的下邊
NSLayoutAttributeLeading 視圖的前邊
NSLayoutAttributeTrailing 視圖的后邊
NSLayoutAttributeWidth 視圖的寬度
NSLayoutAttributeHeight 視圖的高度
NSLayoutAttributeCenterX 視圖的中點的X值
NSLayoutAttributeCenterY 視圖中點的Y值
NSLayoutAttributeBaseline 視圖的基準線
NSLayoutAttributeNotAnAttribute 無屬性

NSLayoutRelation(關系屬性)

NSLayoutRelation的類型 含義
NSLayoutRelationLessThanOrEqual 視圖關系小于或等于
NSLayoutRelationEqual 視圖關系等于
NSLayoutRelationGreaterThanOrEqual 視圖關系大于或等于

代碼提示

<#被約束視圖#>.translatesAutoresizingMaskIntoConstraints = NO;
    [<#view的父視圖#> addConstraint:[NSLayoutConstraint constraintWithItem:<#被約束視圖#> attribute:(NSLayoutAttribute<#被約束視圖的布局屬性#>) relatedBy:(NSLayoutRelation<#布局屬性的布局關系#>) toItem:<#參照視圖#> attribute:NSLayoutAttribute<#參照視圖的布局屬性#> multiplier:<#倍數#> constant:<#常數#>]];

代碼示例

- (void)codeLayoutConstraint {
   
   //創(chuàng)建背景圖
   UIView *backgroundView = [[UIView alloc] init];
   backgroundView.backgroundColor = [UIColor yellowColor];
   [self.view addSubview:backgroundView];
   backgroundView.translatesAutoresizingMaskIntoConstraints = NO;
   
   //和父視圖等寬
   [self.view addConstraint:[NSLayoutConstraint constraintWithItem:backgroundView attribute:(NSLayoutAttributeWidth) relatedBy:(NSLayoutRelationEqual) toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
   
   //二分之一高
   [self.view addConstraint:[NSLayoutConstraint constraintWithItem:backgroundView attribute:(NSLayoutAttributeHeight) relatedBy:(NSLayoutRelationEqual) toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.5 constant:0]];
   
   //垂直對齊
   [self.view addConstraint:[NSLayoutConstraint constraintWithItem:backgroundView attribute:(NSLayoutAttributeCenterX) relatedBy:(NSLayoutRelationEqual) toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
   
   //水平對齊
   [self.view addConstraint:[NSLayoutConstraint constraintWithItem:backgroundView attribute:(NSLayoutAttributeCenterY) relatedBy:(NSLayoutRelationEqual) toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
   
   
   
   //創(chuàng)建橙色視圖
   UIView *orangeView = [[UIView alloc] init];
   orangeView.backgroundColor = [UIColor orangeColor];
   [backgroundView addSubview:orangeView];
   orangeView.translatesAutoresizingMaskIntoConstraints = NO;

   //上邊距離父視圖上邊20
   [backgroundView addConstraint:[NSLayoutConstraint constraintWithItem:orangeView attribute:(NSLayoutAttributeTop) relatedBy:(NSLayoutRelationEqual) toItem:backgroundView attribute:NSLayoutAttributeTop multiplier:1 constant:20]];
   
   //下邊距離父視圖下邊減20
   [backgroundView addConstraint:[NSLayoutConstraint constraintWithItem:orangeView attribute:(NSLayoutAttributeBottom) relatedBy:(NSLayoutRelationEqual) toItem:backgroundView attribute:NSLayoutAttributeBottom multiplier:1 constant:-20]];
   
   if (/* DISABLES CODE */ (YES)) {//if和else效果一樣
       //前邊距離父視圖前邊20
       [backgroundView addConstraint:[NSLayoutConstraint constraintWithItem:orangeView attribute:(NSLayoutAttributeLeading) relatedBy:(NSLayoutRelationEqual) toItem:backgroundView attribute:NSLayoutAttributeLeading multiplier:1 constant:20]];
       
       //后邊距離父視圖后邊減100
       [backgroundView addConstraint:[NSLayoutConstraint constraintWithItem:orangeView attribute:(NSLayoutAttributeTrailing) relatedBy:(NSLayoutRelationEqual) toItem:backgroundView attribute:NSLayoutAttributeTrailing multiplier:1 constant:-100]];
   } else {
       //左邊距離父視圖前邊20
       [backgroundView addConstraint:[NSLayoutConstraint constraintWithItem:orangeView attribute:(NSLayoutAttributeLeft) relatedBy:(NSLayoutRelationEqual) toItem:backgroundView attribute:NSLayoutAttributeLeft multiplier:1 constant:20]];
       //右邊距離父視圖后邊減100
       [backgroundView addConstraint:[NSLayoutConstraint constraintWithItem:orangeView attribute:(NSLayoutAttributeRight) relatedBy:(NSLayoutRelationEqual) toItem:backgroundView attribute:NSLayoutAttributeRight multiplier:1 constant:-100]];
   }
   
   
   
   
   
   //創(chuàng)建紅色視圖
   UIView *redView = [[UIView alloc] init];
   redView.backgroundColor = [UIColor redColor];
   [backgroundView addSubview:redView];
   redView.translatesAutoresizingMaskIntoConstraints = NO;
   
   //上邊距離父視圖上邊20
   [backgroundView addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:(NSLayoutAttributeTop) relatedBy:(NSLayoutRelationEqual) toItem:backgroundView attribute:NSLayoutAttributeTop multiplier:1 constant:20]];
   
   //高度44
   [backgroundView addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:44]];
   
   //自身寬高比1:1
   [backgroundView addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:(NSLayoutAttributeHeight) relatedBy:(NSLayoutRelationEqual) toItem:redView attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
   
   if (/* DISABLES CODE */ (NO)) {//if和else效果一樣
       //左邊距離橙色視圖右邊20
       [backgroundView addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:(NSLayoutAttributeLeft) relatedBy:(NSLayoutRelationEqual) toItem:orangeView attribute:NSLayoutAttributeRight multiplier:1 constant:20]];
   } else {
       //前邊距離橙色視圖后邊20
       [backgroundView addConstraint:[NSLayoutConstraint constraintWithItem:redView attribute:(NSLayoutAttributeLeading) relatedBy:(NSLayoutRelationEqual) toItem:orangeView attribute:NSLayoutAttributeTrailing multiplier:1 constant:20]];
   }
}

自動布局方法2(了解即可)

     @param format 使用VFL格式化的字符串力细,可以參見官方的幫助文檔贪绘;
     @param opts 指定VFL中所有對象的布局屬性和方向净响。舉例:有2個視圖使用VFL進行布局,可以使用NSLayoutFormatAlignAllLeft碎连,就讓兩個視圖左對齊;
     @param metrics 度量或者指標的字典累驮,字典里面有相關的鍵值對來控制相關的度量指標卫枝,通過key獲燃灞;
     @param views 指定約束的視圖:一個或多個剃盾。
    + (NSArray<__kindof NSLayoutConstraint *> *)constraintsWithVisualFormat:(NSString *)format
                                                                    options:(NSLayoutFormatOptions)opts
                                                                    metrics:(nullable NSDictionary<NSString *,id> *)metrics
                                                                      views:(NSDictionary<NSString *, id> *)views;

VFL字符串說明:

VFL字符串說明.png

代碼示例

- (void)codeLayoutConstraint2 {
    
    UIView *yellowView = [[UIView alloc] init];
    yellowView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:yellowView];
    yellowView.translatesAutoresizingMaskIntoConstraints = NO;
    
    UIView *orangeView = [[UIView alloc] init];
    orangeView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:orangeView];
    orangeView.translatesAutoresizingMaskIntoConstraints = NO;
    
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
    redView.translatesAutoresizingMaskIntoConstraints = NO;
    
    
    //創(chuàng)建VFL約束字符串
    
    //水平順序:父視圖腺占,space個距離,黃色寬100痒谴,5個距離衰伯,橙色視圖,space1個距離积蔚,父視圖
    NSString *hVFL = @"H:|-space-[yellowView(100)]-5-[orangeView]-space1-|";
    
    //水平順序:父視圖意鲸,space個距離,紅色視圖寬和橙色視圖相等
    NSString *hVFL1 = @"H:|-space-[redView(==orangeView)]";
    
    //垂直順序:父視圖尽爆,100個距離怎顾,橙色視圖高100
    NSString *vVFL = @"V:|-100-[orangeView(==100)]";
    
    //垂直順序:父視圖,100個距離漱贱,黃色視圖高100槐雾,5個距離,紅色視圖和橙色視圖高相等
    NSString *vVFL1 = @"V:|-100-[yellowView(==100)]-5-[redView(==orangeView)]";
    
    
    //度量值
    NSDictionary *metircs = @{@"space":@15,@"space1":@30};
    //views
    NSDictionary *views = NSDictionaryOfVariableBindings(yellowView,orangeView,redView);
    
    //創(chuàng)建約束
    NSArray *hconstraint = [NSLayoutConstraint constraintsWithVisualFormat:hVFL options:NSLayoutFormatDirectionLeadingToTrailing metrics:metircs views:views];
    
    NSArray *hconstraint1 = [NSLayoutConstraint constraintsWithVisualFormat:hVFL1 options:NSLayoutFormatAlignAllLeft | NSLayoutFormatAlignAllTop metrics:metircs views:views];
    
    NSArray *vconstraint = [NSLayoutConstraint constraintsWithVisualFormat:vVFL options:NSLayoutFormatDirectionLeadingToTrailing metrics:metircs views:views];
    
    NSArray *vconstraint1 = [NSLayoutConstraint constraintsWithVisualFormat:vVFL1 options:NSLayoutFormatDirectionLeadingToTrailing metrics:metircs views:views];
    
    //添加約束
    [self.view addConstraints:hconstraint];
    
    [self.view addConstraints:hconstraint1];
    
    [self.view addConstraints:vconstraint];
    
    [self.view addConstraints:vconstraint1];
    
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末幅狮,一起剝皮案震驚了整個濱河市募强,隨后出現的幾起案子,更是在濱河造成了極大的恐慌崇摄,老刑警劉巖擎值,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異逐抑,居然都是意外死亡鸠儿,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進店門厕氨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來进每,“玉大人汹粤,你說我怎么就攤上這事∑纷罚” “怎么了玄括?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵冯丙,是天一觀的道長肉瓦。 經常有香客問我,道長胃惜,這世上最難降的妖魔是什么泞莉? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮船殉,結果婚禮上鲫趁,老公的妹妹穿的比我還像新娘。我一直安慰自己利虫,他們只是感情好挨厚,可當我...
    茶點故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著糠惫,像睡著了一般疫剃。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上硼讽,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天巢价,我揣著相機與錄音,去河邊找鬼固阁。 笑死壤躲,一個胖子當著我的面吹牛,可吹牛的內容都是我干的备燃。 我是一名探鬼主播碉克,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼并齐!你這毒婦竟也來了漏麦?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤冀膝,失蹤者是張志新(化名)和其女友劉穎唁奢,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體窝剖,經...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡麻掸,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現自己被綠了赐纱。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片脊奋。...
    茶點故事閱讀 39,981評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡熬北,死狀恐怖,靈堂內的尸體忽然破棺而出诚隙,到底是詐尸還是另有隱情讶隐,我是刑警寧澤,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布久又,位于F島的核電站巫延,受9級特大地震影響,放射性物質發(fā)生泄漏地消。R本人自食惡果不足惜炉峰,卻給世界環(huán)境...
    茶點故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望脉执。 院中可真熱鬧疼阔,春花似錦、人聲如沸半夷。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽巫橄。三九已至淘邻,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間嗦随,已是汗流浹背列荔。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留枚尼,地道東北人贴浙。 一個月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像署恍,于是被迫代替她去往敵國和親崎溃。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,933評論 2 355