BYViewConstraint 是一个方便的类,用于在 Objective-C 中实现以下“Auto Layout”。
UILabel* topLabel = [self addLabel:@"fullWidth/top" backColor:[UIColor redColor]];
UILabel* bottomLabel = [self addLabel:@"fullWidth/bottom" backColor:[UIColor redColor]];
UILabel* centerLabel = [self addLabel:@"centerX/Y" backColor:[UIColor blueColor]];
UILabel* leftLabel = [self addLabel:@"left" backColor:[UIColor greenColor]];
UILabel* rightLabel = [self addLabel:@"right" backColor:[UIColor greenColor]];
BYViewConstraint* constraint = [[BYViewConstraint alloc] initWithSuperview:self.view];
[constraint view:topLabel];
[constraint fullWidth];
[constraint top];
[constraint height:50];
[constraint view:bottomLabel];
[constraint fullWidth];
[constraint bottom];
[constraint height:50];
[constraint view:centerLabel];
[constraint centerX];
[constraint centerY];
[constraint width:150];
[constraint height:150];
[constraint view:leftLabel];
[constraint attachTop:topLabel];
[constraint attachBottom:bottomLabel];
[constraint left];
[constraint attachRight:centerLabel];
[constraint view:rightLabel];
[constraint attachTop:topLabel];
[constraint attachBottom:bottomLabel];
[constraint attachLeft:centerLabel];
[constraint right];
[constraint apply];
或在 Swift 中。
let topLabel = addLabel("fullWidth/top", backColor:UIColor.redColor())
let bottomLabel = addLabel("fullWidth/bottom", backColor:UIColor.redColor())
let centerLabel = addLabel("centerX/Y", backColor:UIColor.blueColor())
let leftLabel = addLabel("left", backColor:UIColor.greenColor())
let rightLabel = addLabel("right", backColor:UIColor.greenColor())
let constraint = BYViewConstraint(superview:self.view)
constraint.view(topLabel)
.fullWidth()
.top()
.height(50)
constraint.view(bottomLabel)
.fullWidth()
.bottom()
.height(50)
constraint.view(centerLabel)
.centerX()
.centerY()
.width(150)
.height(150)
constraint.view(leftLabel)
.attachTop(topLabel)
.attachBottom(bottomLabel)
.left()
.attachRight(centerLabel)
constraint.view(rightLabel)
.attachTop(topLabel)
.attachBottom(bottomLabel)
.attachLeft(centerLabel)
.right()
constraint.apply()
要运行示例项目,首先从仓库中克隆,然后从 Example 目录中运行 pod install
。
如果您通过 CocoaPods 安装此库,请从 https://github.com/moicci/BYViewConstraint 克隆 Example 的 repo。
然后安装 pod。
pod install
import "BYViewConstraint.h"
添加到您的 Objective-C 桥接头文件 (-Bridging-Header.h)。#import "BYViewConstraint.h"
。Moicci 在 blevlo media inc。
BYViewConstraint 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。