BLSmartLayoutEngine
是一个适用于 iOS 的轻量级布局引擎。
BLSmartLayoutEngine
是一个适用于 iOS 的轻量级布局引擎,以下是我们创建它的原因:
1: 现在人们使用的某些布局引擎,例如自动调整大小、自动布局和 Masonry,比较复杂。
2: BLSmartLayoutEngine 容易学习和使用,与自动布局不同,它不依赖于任何高级系统版本,因此无需考虑系统版本。
3: 在某些场景中,我们可能需要动态地布局 UI。
iOS 7.0 或更高版本
使用水平 (HBox) 布局所有子视图,子视图之间的间距为 20.0 像素
UIView* containterView = [[UIView alloc] init];
containterView.bls_layoutType = BLSmartLayoutTypeHBox;
containterView.bls_spacing = 20.0;
for ( int j = 0; j < 4; j++ ) {
UIView* subView =[[UIView alloc] init];
[containterView addSubview:subView];
}
使用垂直 (VBox) 布局所有子视图,子视图之间的间距为 20.0 像素
UIView* containterView = [[UIView alloc] init];
containterView.bls_layoutType = BLSmartLayoutTypeVBox;
containterView.bls_spacing = 20.0;
for ( int j = 0; j < 4; j++ ) {
UIView* subView =[[UIView alloc] init];
[containterView addSubview:subView];
}
使用锚点样式布局子视图
UIView* view = [[UIView alloc] init];
view.bls_layoutType = BLSmartLayoutTypeAnchor;
BLSmartLayoutAnchorInfo* anchorInfo = nil;
//left-top
UILabel* label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = @"lt";
label.backgroundColor = [UIColor redColor];
anchorInfo = [[BLSmartLayoutAnchorInfo alloc] init];
anchorInfo.bls_leftAnchor = UIEdgeInsetsMake(0.0, 2.0, 0.0, 0.0);
anchorInfo.bls_topAnchor = UIEdgeInsetsMake(2.0, 0.0, 0.0, 0.0);
anchorInfo.bls_rightAnchor = UIEdgeInsetsMake(0.0, 20.0, 0.0, 0.0);
anchorInfo.bls_bottomAnchor = UIEdgeInsetsMake(22.0, 0.0, 0.0, 0.0);
label.bls_anchorInfo = anchorInfo;
[view addSubview:label];
//right-top
label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = @"rt";
label.backgroundColor = [UIColor redColor];
anchorInfo = [[BLSmartLayoutAnchorInfo alloc] init];
anchorInfo.bls_leftAnchor = UIEdgeInsetsMake(0.0, 0.0, 0.0, 20.0);
anchorInfo.bls_topAnchor = UIEdgeInsetsMake(2.0, 0.0, 0.0, 0.0);
anchorInfo.bls_rightAnchor = UIEdgeInsetsMake(0.0, 0.0, 0.0, 2.0);
anchorInfo.bls_bottomAnchor = UIEdgeInsetsMake(22.0, 0.0, 0.0, 0.0);
label.bls_anchorInfo = anchorInfo;
[view addSubview:label];
//left-bottom
label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = @"lb";
label.backgroundColor = [UIColor redColor];
anchorInfo = [[BLSmartLayoutAnchorInfo alloc] init];
anchorInfo.bls_leftAnchor = UIEdgeInsetsMake(0.0, 2.0, 0.0, 0.0);
anchorInfo.bls_topAnchor = UIEdgeInsetsMake(0.0, 0.0, 22.0, 0.0);
anchorInfo.bls_rightAnchor = UIEdgeInsetsMake(0.0, 20.0, 0.0, 0.0);
anchorInfo.bls_bottomAnchor = UIEdgeInsetsMake(0.0, 0.0, 2.0, 0.0);
label.bls_anchorInfo = anchorInfo;
[view addSubview:label];
//right-bottom
label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = @"rb";
label.backgroundColor = [UIColor redColor];
anchorInfo = [[BLSmartLayoutAnchorInfo alloc] init];
anchorInfo.bls_leftAnchor = UIEdgeInsetsMake(0.0, 0.0, 0.0, 20.0);
anchorInfo.bls_topAnchor = UIEdgeInsetsMake(0.0, 0.0, 22.0, 0.0);
anchorInfo.bls_rightAnchor = UIEdgeInsetsMake(0.0, 0.0, 0.0, 2.0);
anchorInfo.bls_bottomAnchor = UIEdgeInsetsMake(0.0, 0.0, 2.0, 0.0);
label.bls_anchorInfo = anchorInfo;
[view addSubview:label];
创建自定义布局样式
1: 实现一个继承自 BLSmartLayout
的子类
@interface CustomSmartLayout : BLSmartLayout
+ (void)bls_layoutViews:(UIView*)superView;
@end
@implementation CustomSmartLayout
+ (void)bls_layoutViews:(UIView*)superView {
//TODO:layout subviews
}
@end
2: 注册子类
[UIView bls_registLayoutClass:[CustomSmartLayout class] layoutType:BLSmartLayoutTypeUser + 100];
3: 使用新的布局样式
UIView* view = [[UIView alloc] init];
view.bls_layoutType = BLSmartLayoutTypeUser + 100;
查看示例,您会很快找到方法。