uikit——Auto Layout——UILayoutGuide

UILayoutGuide

传统上,在UILayoutGuide之前,ios使用dummy view实现下述功能:
  • 定义内部view间距,dummy view作为内部view sibling
  • 封装view,对view进行模块化分组,dummy view作为view parent
注:dummy view是一个自身无可视化元素的empty view
dummy view有以下缺点:
  • 创建和维护dummy view自身成本高
  • dummy view本质是view,有正常view具有的一切功能,是view hierarchy完全成员,增加了view hierarchy执行每个任务费用
  • 不可见的dummy view能拦截本应传递给其它view消息,导致程序出现问题,且这类问题很难发现
因为dummy有这些缺点,apple引入UILayoutGuide代替dummy view,UILayoutGuide无dummy view上述缺点,但有dummy view上述功能:
  • 定义内部UI对象间距
  • 封装UI对象,对UI对象进行模块化分组
注:引入UILayoutGuide之前,UI对象即UIView,引入UILayoutGuide之后,UILayoutGuide也是UI对象之一
UILayoutGuide执行这些功能更安全,更有效,UILayoutGuide并非view,因此不是view hierarchy成员,UILayoutGuide仅仅在其owning view坐标系统中定义了一块矩形区域,此矩形区域能与auto layout交互,UILayoutGuide作为一个UI对象,具有UI对象具有的通用属性:
  • leadingAnchor
  • trailingAnchor
  • leftAnchor
  • rightAnchor
  • topAnchor
  • bottomAnchor
  • widthAnchor
  • heightAnchor
  • centerXAnchor
  • centerYAnchor
不管dummy view封装view,还是UILayoutGuide封装UI对象,并不会封装constraint,auto layout依然会比较分组模块内constraint优先级和分组模块外constraint优先级,即所有constraint是统一进行优先级排序
创建UILayoutGuide,分三步:
  • 实例化layout guide
  • 通过view的addLayoutGuide添加到view
  • 使用auto layout定义layout guide position和size
/* UILayoutGuides will not show up in the view hierarchy, but may be used as items in
 an NSLayoutConstraint and represent a rectangle in the layout engine.
 
 Create a UILayoutGuide with -init, and add to a view with -[UIView addLayoutGuide:]
 before using it in a constraint.
 */

@interface UILayoutGuide : NSObject <NSCoding>

/* The frame of the UILayoutGuide in its owningView's coordinate system.
 Valid by the time the owningView receives -layoutSubviews.
 */
@property(nonatomic,readonly) CGRect layoutFrame;

/* The guide must be added to a view with -[UIView addLayoutGuide:] before being used in a constraint.
 Do not use this property directly to change the owningView of a layout guide. Instead, use 
 -[UIView addLayoutGuide:] and -[UIView removeLayoutGuide:], which will use this property to 
 change the owningView.
 */
@property(nonatomic,weak,nullable) UIView *owningView;

/* For ease of debugging.
 'UI' prefix is reserved for UIKit-created layout guides.
 */
@property(nonatomic,copy) NSString *identifier;


/* Constraint creation conveniences. See NSLayoutAnchor.h for details.
 */
@property(readonly, strong) NSLayoutXAxisAnchor *leadingAnchor;
@property(readonly, strong) NSLayoutXAxisAnchor *trailingAnchor;
@property(readonly, strong) NSLayoutXAxisAnchor *leftAnchor;
@property(readonly, strong) NSLayoutXAxisAnchor *rightAnchor;
@property(readonly, strong) NSLayoutYAxisAnchor *topAnchor;
@property(readonly, strong) NSLayoutYAxisAnchor *bottomAnchor;
@property(readonly, strong) NSLayoutDimension *widthAnchor;
@property(readonly, strong) NSLayoutDimension *heightAnchor;
@property(readonly, strong) NSLayoutXAxisAnchor *centerXAnchor;
@property(readonly, strong) NSLayoutYAxisAnchor *centerYAnchor;

@end
解释:
  • identifier方便调试,NS或UI开头identifier为系统所保留
  • owningView为UILayoutGuide拥有者,不要直接修改值,调用addLayoutGuide或removeLayoutGuide更新值
  • layoutFrame参考坐标系为owningView坐标系,在调用owningView的layoutSubviews时才有valid值
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值