文檔解釋
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é)果:
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;
}
當(dāng)yellowView.preservesSuperviewLayoutMargins = NO時:
將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的邊距:
見demo [self example3];