XLYAutoLayoutEasy 2.0.0

XLYAutoLayoutEasy 2.0.0

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2015年5月

kaizeiyimi 维护。



  • kaizei

升级到2.0!

增加了处理 direction 的新功能,重构了所有以 xly_ 开头的API和一些其他改进。

XCode 6.3及以上版本

在公共头文件中使用 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)。

查看示例或源代码以获取更多详细信息。代码易于理解和使用。

AutoLayout代表着什么?

  • API对开发人员不够友好。至少我认为是这样。

您可以使用+[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]来创建约束,并在需要时修改优先级。创建约束后,您需要负责找到适当的视图来添加约束。要添加约束的视图必须是约束中firstItem和secondItem的共同父视图。API太长,寻找共同父视图相当繁琐。

或者,您可以使用VFL来创建约束。听起来不错,但问题依然存在。您仍然需要找到共同父视图(尽管,有人只是使用viewController.view),您在返回的数组中获取指定约束时也会遇到问题。

我所简化内容

简而言之,我制作了这个库,旨在让autolayout的代码编写更加容易。但应该简化到什么程度呢?经过我的思考,目前我只简化了+[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:] API。提供了makeupdate函数来帮助创建、激活和修改约束。

现在,我还添加了sizecenteredge复合约束,使代码更加简便易懂。