1. 程式人生 > >uikit——Auto Layout——UIView——anchor

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