uikit——Auto Layout——UIView——margin

本文介绍了在UIKit中Auto Layout与UIView的Margin属性,包括`margin`、`layoutMargins`、`layoutMarginsDidChange`和`preservesSuperviewLayoutMargins`。`layoutMargins`默认值及其在不同场景下的应用,特别是视图控制器根视图的特殊设置,开发者无法直接修改根视图的`layoutMargins`。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值