增加了处理 direction 的新功能,重构了所有以 xly_ 开头的API和一些其他改进。
在公共头文件中使用 nullabity 注解(主要用于Swift),因此您应该使用XCode 6.3及以上版本。有关详细信息,请参阅 Apple的Swift博客。
//objective-c code
// import "XLYAutoLayoutEasy.h"
//make constraints. Put code in the block, the constraints will auto active.
// default uses NSLayoutFormatOptions.DirectionLeadingToTrailing
[NSLayoutConstraint xly_make:^{
// set width equal to (titleLabel's height), height equal to 60
[self.imageView.xly_width equalToConstant:self.titleLabel.xly_height];
[self.imageView.xly_height equalToConstant:60];
// you can use composite format
[self.imageView.xly_size equalToWidth:self.titleLabel.xly_height height:@60];
// if width and height are all numbers, you can use CGSize format
[self.imageView.xly_size equalToCGSize:CGSizeMake(60, 60)];
}];
// we also support center and edge composite constraints. see API for more detail.
//update
- (void)updateConstraints {
const CGFloat margin = 8;
// these codes show a recommended place to use updateConstraints. 'update' will auto find the 'similiar' constraint, disable it and active new one.
[NSLayoutConstraint xly_update:^{
[_leftView.xly_edge equalToTop:@(margin) leading:@(margin) bottom:@(-margin) trailing:nil];
[_leftView.xly_width equalTo:self.xly_width.multiplier(0.5).constant(-1.5 * margin)];
[_rightView.xly_edge equalToTop:_leftView leading:_leftView.xly_trailing.constant(margin) bottom:_leftView trailing:@(-margin)];
}];
[super updateConstraints];
}
//swift code
// import "XLYAutoLayoutEasy-swift.h"
// It's almost the same as OC style.
// default uses NSLayoutFormatOptions.DirectionLeadingToTrailing
NSLayoutConstraint.xly_make {
self.centerView.xly_size.equalTo(self.titleLabel, offset: UIOffset(horizontal: 0, vertical: 20))
self.centerView.xly_center.equalToCenterX(self.contentLabel.xly_centerX.constant(10), centerY: self.view)
// use right-to-left to make leading and trailing to be right and left.
NSLayoutConstraint.xly_makeWithDirection(NSLayoutFormatOptions.DirectionRightToLeft) {
self.purpleView.xly_edge.equalTo(self.centerView, insets: UIEdgeInsets(top: 5, left: 20, bottom: 5, right: 5))
}
}
它很容易阅读和编写。不是吗?
Swift编译器有一个bug,即 UILayoutPriority
变量不能出现在代码中,否则编译会失败。因此,目前我们必须在代码中使用750来替换 UILayoutPriorityDefaultHigh
。
与某些其他库不同,我的库 可以与其他方式创建的约束一起工作,例如从Storyboard或xib创建的约束,甚至VFL或先前API创建的约束,与在 +[UIView xly_makeConstraint:] 中创建的约束没有区别,并且可以在 更新 中查找。我做的所有事情只是简化约束的创建。
pod 'XLYAutoLayoutEasy', '~> 2.0.0'。
在您想使用的地方导入 "XLYAutoLayoutEasy.h"
。如果您使用Swift,请在桥接头中导入 "XLYAutoLayoutEasy-swift.h"
。
+[NSLayoutConstraint xly_make:]
创建和激活约束。使用 +[NSLayoutConstraint xly_update:]
查找一个现有类似约束,删除它并激活新的约束,如果没有找到类似约束,则创建并激活新的约束。
xly_XX* 属性,例如xly_height或xly_leading,用于描述视图和 NSLayoutAttribute
属性。
所有期望 'id' 的参数可以是 ViewAttribute(普通或复合)、UIView 或 'Number'。 ViewAttribute 指定一个视图和 NSLayoutAttribute。UIView仅指定一个视图,NSLayoutAttribute将与firstItem的布局属性相同。'Number' 将指定与firstItem具有相同布局属性并具有相同数值的父视图(高度和宽度将设置secondItem为nil和secondAttribute为NSLayoutAttributeNotAnAttribute)。
查看示例或源代码以获取更多详细信息。代码易于理解和使用。
您可以使用+[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]
来创建约束,并在需要时修改优先级。创建约束后,您需要负责找到适当的视图来添加约束。要添加约束的视图必须是约束中firstItem和secondItem的共同父视图。API太长,寻找共同父视图相当繁琐。
或者,您可以使用VFL来创建约束。听起来不错,但问题依然存在。您仍然需要找到共同父视图(尽管,有人只是使用viewController.view),您在返回的数组中获取指定约束时也会遇到问题。
简而言之,我制作了这个库,旨在让autolayout
的代码编写更加容易。但应该简化到什么程度呢?经过我的思考,目前我只简化了+[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]
API。提供了make
和update
函数来帮助创建、激活和修改约束。
现在,我还添加了size
、center
和edge
复合约束,使代码更加简便易懂。