layoutMargins和preservesSuperviewLayoutMargins

文檔解釋

layoutMargins

使用這個屬性用于指定視圖和它的子視圖之間的邊距(單位使用點),自動布局系統(tǒng)使用你的margins作為限制條件布局內(nèi)容。例如恤批,如果你通過格式化語句“|-[subview]-|”設(shè)置水平約束禁悠,則子視圖的左右邊緣距離父視圖左右內(nèi)邊緣相應(yīng)的layout margins昔穴。當(dāng)你的視圖邊緣接近父視圖的邊緣并且你視圖的 preservesSuperviewLayoutMargins屬性為YES的時候料仗,實際的layout margins可能會增加以防止內(nèi)容與superview的邊距重疊遵岩。

默認(rèn)的margins 在每個邊緣都是8 points

如果視圖是一個控制器的root view鸭巴,系統(tǒng)設(shè)置管理視圖的margins眷细,頂部和底部的marigns被設(shè)置為0。側(cè)面的margins依賴于當(dāng)前size class而變化鹃祖,但可以使16或者20points溪椎。你不可以更改這些margins

preservesSuperviewLayoutMargins

當(dāng)這個屬性的值為YES的時候,一個視圖布局內(nèi)容時其父視圖的margins也會被考慮在內(nèi)。這個margins對于一個視圖與其父視圖邊緣距離的影響小于視圖相應(yīng)的margin池磁。例如奔害,你有一個內(nèi)容視圖的frame恰好和父視圖的bounds一樣,當(dāng)父視圖的任何margins在內(nèi)容視圖和它的margins的表示范圍的內(nèi)部時(有重合地熄?华临??)端考,UIKit自動調(diào)整內(nèi)容視圖的的布局以滿足父視圖的margins雅潭。調(diào)整的數(shù)量是保證內(nèi)容視圖恰好在父視圖的margins范圍內(nèi)的最小數(shù)量。

屬性默認(rèn)值為NO却特。

個人理解

layoutMargins

設(shè)置一個視圖的邊距(視圖邊緣與其子視圖邊緣的距離)扶供,防止子視圖和父視圖邊緣重合。在iOS 8中裂明,可以使用layoutMargins去定義view之間的間距,該屬性只對AutoLayout布局生效椿浓。

因此AutoLayout中NSLayoutAttribute的枚舉值有了相應(yīng)的更新:

 NSLayoutAttributeLeftMargin NS_ENUM_AVAILABLE_IOS(8_0),
 NSLayoutAttributeRightMargin NS_ENUM_AVAILABLE_IOS(8_0),
 NSLayoutAttributeTopMargin NS_ENUM_AVAILABLE_IOS(8_0),
 NSLayoutAttributeBottomMargin NS_ENUM_AVAILABLE_IOS(8_0),
 NSLayoutAttributeLeadingMargin NS_ENUM_AVAILABLE_IOS(8_0),
 NSLayoutAttributeTrailingMargin NS_ENUM_AVAILABLE_IOS(8_0),
 NSLayoutAttributeCenterXWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),
 NSLayoutAttributeCenterYWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),

代碼只有通過這些枚舉值定義約束時,父視圖定義的layoutMargins才會生效闽晦。見[self example1];消息

代碼:

- (void)example1 {
    UIView *blueView = [[UIView alloc] init];
    blueView.backgroundColor = [UIColor blueColor];
    blueView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:blueView];
    NSMutableArray *constraints = [NSMutableArray array];
    [constraints addObject:[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0]];
    [constraints addObject:[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0.0]];
    [constraints addObject:[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
    [constraints addObject:[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
    [self.view addConstraints:constraints];
    
    UIView *yellowView = [[UIView alloc] init];
    yellowView.backgroundColor = [UIColor yellowColor];
    yellowView.translatesAutoresizingMaskIntoConstraints = NO;
    [blueView addSubview:yellowView];
    [constraints removeAllObjects];
    [constraints addObject:[NSLayoutConstraint constraintWithItem:yellowView attribute:NSLayoutAttributeLeftMargin relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeLeftMargin multiplier:1.0 constant:0.0]];
    [constraints addObject:[NSLayoutConstraint constraintWithItem:yellowView attribute:NSLayoutAttributeRightMargin relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeRightMargin multiplier:1.0 constant:0.0]];
    [constraints addObject:[NSLayoutConstraint constraintWithItem:yellowView attribute:NSLayoutAttributeTopMargin relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeTopMargin multiplier:1.0 constant:0.0]];
    [constraints addObject:[NSLayoutConstraint constraintWithItem:yellowView attribute:NSLayoutAttributeBottomMargin relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeBottomMargin multiplier:1.0 constant:0.0]];
    [blueView addConstraints:constraints];

    blueView.layoutMargins = UIEdgeInsetsMake(50, 50, 50, 50);
}

結(jié)果:


6C50DE61-ABB2-42C9-B21D-E427093BA09D.png

preservesSuperviewLayoutMargins

當(dāng)為YES時扳碍,保證滿足父視圖通過layoutMargins設(shè)置的邊距,如果一個視圖A本身無法滿足父視圖B的layoutMargins并且視圖的preservesSuperviewLayoutMargins屬性為YES時仙蛉,UIKit自動調(diào)整視圖A內(nèi)容視圖的的布局以滿足父視圖的margins笋敞。
見[self example2];

設(shè)置blueView的layoutMargins都為50,yellowView高為blueView的一半且中心點重合荠瘪,UIKit約束計算后得出yellowView此時上下邊緣到blueView的上下邊緣距離滿足50 points距離的結(jié)論夯巷。但是左右邊緣不滿足所以當(dāng)yellowView的preservesSuperviewLayoutMargins為YES時,UIKit自動調(diào)整blackView的左右邊距以滿足blueView的margins哀墓。當(dāng)yellowView.preservesSuperviewLayoutMargins = NO;的時候blackView完全遮蓋yellowView

代碼:

- (void)example2 {
        UIView *blueView = [[UIView alloc] init];
        blueView.backgroundColor = [UIColor blueColor];
        blueView.translatesAutoresizingMaskIntoConstraints = NO;
        [self.view addSubview:blueView];
        NSMutableArray *constraints = [NSMutableArray array];
        [constraints addObject:[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0]];
        [constraints addObject:[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0.0]];
        [constraints addObject:[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
        [constraints addObject:[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
        [self.view addConstraints:constraints];
    
        UIView *yellowView = [[UIView alloc] init];
        yellowView.backgroundColor = [UIColor yellowColor];
        yellowView.translatesAutoresizingMaskIntoConstraints = NO;
        [blueView addSubview:yellowView];
        [constraints removeAllObjects];
        [constraints addObject:[NSLayoutConstraint constraintWithItem:yellowView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0]];
        [constraints addObject:[NSLayoutConstraint constraintWithItem:yellowView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeHeight multiplier:0.5 constant:0.0]];
        [constraints addObject:[NSLayoutConstraint constraintWithItem:yellowView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
        [constraints addObject:[NSLayoutConstraint constraintWithItem:yellowView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
        [blueView addConstraints:constraints];
    
        UIView *blackView = [[UIView alloc] init];
        blackView.backgroundColor = [UIColor blackColor];
        blackView.translatesAutoresizingMaskIntoConstraints = NO;
        [yellowView addSubview:blackView];
        [constraints removeAllObjects];
        [constraints addObject:[NSLayoutConstraint constraintWithItem:blackView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:yellowView attribute:NSLayoutAttributeTrailingMargin multiplier:1.0 constant:0.0]];
        [constraints addObject:[NSLayoutConstraint constraintWithItem:blackView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:yellowView attribute:NSLayoutAttributeLeadingMargin multiplier:1.0 constant:0.0]];
        [constraints addObject:[NSLayoutConstraint constraintWithItem:blackView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:yellowView attribute:NSLayoutAttributeTopMargin multiplier:1.0 constant:0.0]];
        [constraints addObject:[NSLayoutConstraint constraintWithItem:blackView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:yellowView attribute:NSLayoutAttributeBottomMargin multiplier:1.0 constant:0.0]];
        [yellowView addConstraints:constraints];
    

        // 設(shè)置blueView的layoutMargins都為50趁餐,yellowView高為blueView的一半且中心點重合,UIKit約束計算后得出yellowView此時上下邊緣到blueView的上下邊緣距離滿足50 points距離的結(jié)論篮绰。但是左右邊緣不滿足所以當(dāng)yellowView的preservesSuperviewLayoutMargins為YES時后雷,UIKit自動調(diào)整blackView的左右邊距以滿足blueView的margins。當(dāng)yellowView.preservesSuperviewLayoutMargins = NO;的時候blackView完全遮蓋yellowView
        blueView.layoutMargins = UIEdgeInsetsMake(50, 50, 50, 50);
        yellowView.layoutMargins = UIEdgeInsetsZero;
        yellowView.preservesSuperviewLayoutMargins = YES;
}
656D2D8A-1B6E-4CE7-9482-9C2E5B3319C1.png

當(dāng)yellowView.preservesSuperviewLayoutMargins = NO時:

944992DD-1582-4349-A0C2-B53AC147E571.png

將yellowView.preservesSuperviewLayoutMargins 設(shè)置為YES阶牍,修改:

[constraints addObject:[NSLayoutConstraint constraintWithItem:yellowView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];

為:

[constraints addObject:[NSLayoutConstraint constraintWithItem:yellowView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];

后yellowView的頂部邊緣和blueView的頂部邊緣重合,不在滿足blueView設(shè)置的
layoutMargins的值星瘾,所以UIKit自動調(diào)整blackView的邊距:

E27D0CAC-F6C7-41E6-A2A7-C62906D29D49.png

見demo [self example3];

demo地址

參考:細(xì)數(shù)AutoLayout以來UIView和UIViewController新增的相關(guān)API – UIView篇

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末走孽,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子琳状,更是在濱河造成了極大的恐慌磕瓷,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異困食,居然都是意外死亡边翁,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進(jìn)店門硕盹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來符匾,“玉大人,你說我怎么就攤上這事瘩例“〗海” “怎么了?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵垛贤,是天一觀的道長焰坪。 經(jīng)常有香客問我,道長聘惦,這世上最難降的妖魔是什么某饰? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮善绎,結(jié)果婚禮上黔漂,老公的妹妹穿的比我還像新娘。我一直安慰自己涂邀,他們只是感情好瘟仿,可當(dāng)我...
    茶點故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著比勉,像睡著了一般劳较。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上浩聋,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天观蜗,我揣著相機(jī)與錄音,去河邊找鬼衣洁。 笑死墓捻,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的坊夫。 我是一名探鬼主播砖第,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼环凿!你這毒婦竟也來了梧兼?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤智听,失蹤者是張志新(化名)和其女友劉穎羽杰,沒想到半個月后渡紫,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡考赛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年惕澎,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片颜骤。...
    茶點故事閱讀 39,981評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡唧喉,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出复哆,到底是詐尸還是另有隱情欣喧,我是刑警寧澤,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布梯找,位于F島的核電站唆阿,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏锈锤。R本人自食惡果不足惜驯鳖,卻給世界環(huán)境...
    茶點故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望久免。 院中可真熱鬧浅辙,春花似錦、人聲如沸阎姥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽呼巴。三九已至泽腮,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間衣赶,已是汗流浹背诊赊。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留府瞄,地道東北人碧磅。 一個月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像遵馆,于是被迫代替她去往敵國和親鲸郊。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,933評論 2 355

推薦閱讀更多精彩內(nèi)容