天天看点

uikit——Auto Layout——UIView——anchor

anchor

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

@end           

解释:

  • leadingAnchor:NSLayoutAttributeLeading
  • trailingAnchor:NSLayoutAttributeTrailing
  • leftAnchor:NSLayoutAttributeLeft
  • rightAnchor:NSLayoutAttributeRight
  • topAnchor:NSLayoutAttributeTop
  • bottomAnchor:NSLayoutAttributeBottom
  • widthAnchor:NSLayoutAttributeWidth
  • heightAnchor:NSLayoutAttributeHeight
  • centerXAnchor:NSLayoutAttributeCenterX
  • centerYAnchor:NSLayoutAttributeCenterY
  • firstBaselineAnchor:NSLayoutAttributeFirstBaseline
  • lastBaselineAnchor:NSLayoutAttributeLastBaseline

view anchor属性可用来方便建立constraint,各个anchor属性有各自正确anchor类型,只有同一类型anchor才能生成valid constraint,但不确保生成的constraint一定valid,比如使用leadingAnchor和leftAnchor生成constraint,因为leadingAnchor和leftAnchor都为NSLayoutXAxisAnchor,因此语法允许,但auto layout逻辑上不允许,这会导致runtime crash 注:NSLayoutXAxisAnchor,NSLayoutYAxisAnchor,NSLayoutDimension会进行参数类型检查,若不同类型,编译器warning