今天我们来说说约束,之前一直使用的都是masonry.后来经过朋友推荐换成了purelayout.发现 purelayout比masonry用起来简单的多.方便的多.由于很多朋友还在使用frame,故写了这篇约束的文章.
github:
https://github.com/PureLayout/PureLayout
复制代码
首先,约束写在哪?masonry的约束可以直接写在addsubview之后. purelayouut当然也可以,不过为了代码的简洁性.最好还是重写updateViewConstraints. 先定义一个数组
@property (nonatomic, retain) NSArray *contentConstrains;
复制代码
然后实现
- (void)updateViewConstraints{
if(_contentConstrains != nil){
[_contentConstrains autoRemoveConstraints];
}
_contentConstrains = [NSLayoutConstraint autoCreateConstraintsWithoutInstalling:^{
[self setupConstraints];
}];
[_contentConstrains autoInstallConstraints];
[super updateViewConstraints];
}
复制代码
这样,这样 只需要重写setupConstraints就好了.
purelayout方法翻译如下
/** view在父类view的中心. */
- (__NSArray_of(NSLayoutConstraint *) *)autoCenterInSuperview;
/** 父类的X或者Y轴对齐. */
- (NSLayoutConstraint *)autoAlignAxisToSuperviewAxis:(ALAxis)axis;
#if __PureLayout_MinBaseSDK_iOS_8_0
/** 父类边缘的中心. iOS8.0之后可用. */
- (__NSArray_of(NSLayoutConstraint *) *)autoCenterInSuperviewMargins;
/** 父类轴边缘对齐. iOS8.0之后可用. */
- (NSLayoutConstraint *)autoAlignAxisToSuperviewMarginAxis:(ALAxis)axis;
#endif /* __PureLayout_MinBaseSDK_iOS_8_0 */
#pragma mark Pin Edges to Superview
/** 边缘与父view对齐 */
- (NSLayoutConstraint *)autoPinEdgeToSuperviewEdge:(ALEdge)edge;
/**边缘与父view对齐,距离为inset. */
- (NSLayoutConstraint *)autoPinEdgeToSuperviewEdge:(ALEdge)edge withInset:(CGFloat)inset;
/** 边缘与父类对齐,距离为inset 关系为大于,小于或者等于*/
- (NSLayoutConstraint *)autoPinEdgeToSuperviewEdge:(ALEdge)edge withInset:(CGFloat)inset relation:(NSLayoutRelation)relation;
/**四周与父类四周对齐. */
- (__NSArray_of(NSLayoutConstraint *) *)autoPinEdgesToSuperviewEdges;
/**四周与父类四周对齐.距离为UIEdgeInsets. */
- (__NSArray_of(NSLayoutConstraint *) *)autoPinEdgesToSuperviewEdgesWithInsets:(ALEdgeInsets)insets;
/**四周与父类四周对齐,距离为UIedgeInsets, 除edge意外.. */
- (__NSArray_of(NSLayoutConstraint *) *)autoPinEdgesToSuperviewEdgesWithInsets:(ALEdgeInsets)insets excludingEdge:(ALEdge)edge;
#if __PureLayout_MinBaseSDK_iOS_8_0
/** Pins the given edge of the view to the corresponding margin of its superview. Available in iOS 8.0 and later. */
- (NSLayoutConstraint *)autoPinEdgeToSuperviewMargin:(ALEdge)edge;
/** Pins the given edge of the view to the corresponding margin of its superview as a maximum or minimum. Available in iOS 8.0 and later. */
- (NSLayoutConstraint *)autoPinEdgeToSuperviewMargin:(ALEdge)edge relation:(NSLayoutRelation)relation;
/** Pins the edges of the view to the margins of its superview. Available in iOS 8.0 and later. */
- (__NSArray_of(NSLayoutConstraint *) *)autoPinEdgesToSuperviewMargins;
/** Pins 3 of the 4 edges of the view to the margins of its superview excluding one edge. Available in iOS 8.0 and later. */
- (__NSArray_of(NSLayoutConstraint *) *)autoPinEdgesToSuperviewMarginsExcludingEdge:(ALEdge)edge;
#endif /* __PureLayout_MinBaseSDK_iOS_8_0 */
#pragma mark Pin Edges
/** 自己的edge与otherView的toEdge对齐. */
- (NSLayoutConstraint *)autoPinEdge:(ALEdge)edge toEdge:(ALEdge)toEdge ofView:(ALView *)otherView;
/** 自己的edge与otherView的toEdge对齐 距离为offset. */
- (NSLayoutConstraint *)autoPinEdge:(ALEdge)edge toEdge:(ALEdge)toEdge ofView:(ALView *)otherView withOffset:(CGFloat)offset;
/** 自己的edge与otherView的toEdge对齐 距离为offset . 关系为relation*/
- (NSLayoutConstraint *)autoPinEdge:(ALEdge)edge toEdge:(ALEdge)toEdge ofView:(ALView *)otherView withOffset:(CGFloat)offset relation:(NSLayoutRelation)relation;
#pragma mark Align Axes
/** 自己的axis轴与otherView的轴对齐. */
- (NSLayoutConstraint *)autoAlignAxis:(ALAxis)axis toSameAxisOfView:(ALView *)otherView;
/** 自己的axis轴与otherView的轴对齐 距离为offset. */
- (NSLayoutConstraint *)autoAlignAxis:(ALAxis)axis toSameAxisOfView:(ALView *)otherView withOffset:(CGFloat)offset;
/** Aligns an axis of the view to the same axis of another view with a multiplier. */
- (NSLayoutConstraint *)autoAlignAxis:(ALAxis)axis toSameAxisOfView:(ALView *)otherView withMultiplier:(CGFloat)multiplier;
#pragma mark Match Dimensions
/** Matches a dimension of the view to a given dimension of another view. */
- (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(ALView *)otherView;
/** Matches a dimension of the view to a given dimension of another view with an offset. */
- (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(ALView *)otherView withOffset:(CGFloat)offset;
/** Matches a dimension of the view to a given dimension of another view with an offset as a maximum or minimum. */
- (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(ALView *)otherView withOffset:(CGFloat)offset relation:(NSLayoutRelation)relation;
/** Matches a dimension of the view to a multiple of a given dimension of another view. */
- (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(ALView *)otherView withMultiplier:(CGFloat)multiplier;
/** Matches a dimension of the view to a multiple of a given dimension of another view as a maximum or minimum. */
- (NSLayoutConstraint *)autoMatchDimension:(ALDimension)dimension toDimension:(ALDimension)toDimension ofView:(ALView *)otherView withMultiplier:(CGFloat)multiplier relation:(NSLayoutRelation)relation;
#pragma mark Set Dimensions
/** Sets the view to a specific size. */
- (__NSArray_of(NSLayoutConstraint *) *)autoSetDimensionsToSize:(CGSize)size;
/** Sets the given dimension of the view to a specific size. */
- (NSLayoutConstraint *)autoSetDimension:(ALDimension)dimension toSize:(CGFloat)size;
/** Sets the given dimension of the view to a specific size as a maximum or minimum. */
- (NSLayoutConstraint *)autoSetDimension:(ALDimension)dimension toSize:(CGFloat)size relation:(NSLayoutRelation)relation;
#pragma mark Set Content Compression Resistance & Hugging
/** Sets the priority of content compression resistance for an axis.
NOTE: This method must be called from within the block passed into the method +[NSLayoutConstraint autoSetPriority:forConstraints:] */
- (void)autoSetContentCompressionResistancePriorityForAxis:(ALAxis)axis;
/** Sets the priority of content hugging for an axis.
NOTE: This method must be called from within the block passed into the method +[NSLayoutConstraint autoSetPriority:forConstraints:] */
- (void)autoSetContentHuggingPriorityForAxis:(ALAxis)axis;
#pragma mark Constrain Any Attributes
/** Constrains an attribute of the view to a given attribute of another view. */
- (NSLayoutConstraint *)autoConstrainAttribute:(ALAttribute)attribute toAttribute:(ALAttribute)toAttribute ofView:(ALView *)otherView;
/** Constrains an attribute of the view to a given attribute of another view with an offset. */
- (NSLayoutConstraint *)autoConstrainAttribute:(ALAttribute)attribute toAttribute:(ALAttribute)toAttribute ofView:(ALView *)otherView withOffset:(CGFloat)offset;
/** Constrains an attribute of the view to a given attribute of another view with an offset as a maximum or minimum. */
- (NSLayoutConstraint *)autoConstrainAttribute:(ALAttribute)attribute toAttribute:(ALAttribute)toAttribute ofView:(ALView *)otherView withOffset:(CGFloat)offset relation:(NSLayoutRelation)relation;
/** Constrains an attribute of the view to a given attribute of another view with a multiplier. */
- (NSLayoutConstraint *)autoConstrainAttribute:(ALAttribute)attribute toAttribute:(ALAttribute)toAttribute ofView:(ALView *)otherView withMultiplier:(CGFloat)multiplier;
/** Constrains an attribute of the view to a given attribute of another view with a multiplier as a maximum or minimum. */
- (NSLayoutConstraint *)autoConstrainAttribute:(ALAttribute)attribute toAttribute:(ALAttribute)toAttribute ofView:(ALView *)otherView withMultiplier:(CGFloat)multiplier relation:(NSLayoutRelation)relation;
#pragma mark Pin to Layout Guides (iOS only)
#if TARGET_OS_IPHONE
/** Pins the top edge of the view to the top layout guide of the given view controller with an inset. Available on iOS only. */
- (NSLayoutConstraint *)autoPinToTopLayoutGuideOfViewController:(UIViewController *)viewController withInset:(CGFloat)inset;
/** Pins the top edge of the view to the top layout guide of the given view controller with an inset as a maximum or minimum. Available on iOS only. */
- (NSLayoutConstraint *)autoPinToTopLayoutGuideOfViewController:(UIViewController *)viewController withInset:(CGFloat)inset relation:(NSLayoutRelation)relation;
/** Pins the bottom edge of the view to the bottom layout guide of the given view controller with an inset. Available on iOS only. */
- (NSLayoutConstraint *)autoPinToBottomLayoutGuideOfViewController:(UIViewController *)viewController withInset:(CGFloat)inset;
/** Pins the bottom edge of the view to the bottom layout guide of the given view controller with an inset as a maximum or minimum. Available on iOS only. */
- (NSLayoutConstraint *)autoPinToBottomLayoutGuideOfViewController:(UIViewController *)viewController withInset:(CGFloat)inset relation:(NSLayoutRelation)relation;
#endif /* TARGET_OS_IPHONE */
复制代码