uikit——Auto Layout——UIView——margin

margin

/*
 -layoutMargins returns a set of insets from the edge of the view's bounds that denote a default spacing for laying out content.
 If preservesSuperviewLayoutMargins is YES, margins cascade down the view tree, adjusting for geometry offsets, so that setting the left value of layoutMargins on a superview will affect the left value of layoutMargins for subviews positioned close to the left edge of their superview's bounds
 If your view subclass uses layoutMargins in its layout or drawing, override -layoutMarginsDidChange in order to refresh your view if the margins change.
 */
@property (nonatomic) UIEdgeInsets layoutMargins NS_AVAILABLE_IOS(8_0);
@property (nonatomic) BOOL preservesSuperviewLayoutMargins NS_AVAILABLE_IOS(8_0); // default is NO - set to enable pass-through or cascading behavior of margins from this view’s parent to its children
- (void)layoutMarginsDidChange NS_AVAILABLE_IOS(8_0);

layoutMargins

layoutMargins定义了view.frame与view.layoutMarginsGuide.layoutFrame之间的各边间接(top,left,bottom,right)
layoutMargins和layoutMarginsGuide是margin的一体两面,本质一致,但只能通过layoutMargins改变margin值,layoutMargins改变layoutMarginsGuide同步改变,始终保持一致,layoutMargins和layoutMarginsGuide之间有下面关系:
  • view.left = layoutMarginsGuide.left - layoutMargins.left
  • view.right = layoutMarginsGuide.right + layoutMargins.right
  • view.top = layoutMarginsGuide.top - layoutMargins.top
  • view.bottom = layoutMarginsGuide.bottom + layoutMargins.bottom
因此margin为正数时,layoutMarginsGuide对应边缘在view内侧,margin为负数时,layoutMarginsGuide对应边缘在view外侧
margin默认值:
  • 通常view边缘默认间距top,left,bottom,right各为8,即layoutMargins为{8, 8, 8, 8}
  • 对于view controller's root view(view属性),layoutMargins由系统设置和管理,出于设计美观考虑,top和bottom边缘默认间距为0,left和right边缘默认间距为16或20,即layoutMargins为{0, 16, 0, 16}或{0, 20, 0, 20},具体哪个值依赖view controller显示方式和显示位置,开发者无法改变其值

layoutMarginsDidChange

layoutMargins改变时调用

preservesSuperviewLayoutMargins

你可能感兴趣的:(uikit)