工具SDAutoLayout,支持Cell和Tableview高度自适应,Label和ScrollView内容自适应。
GitHub地址:https://github.com/gsdios/SDAutoLayout
本Demo:https://download.csdn.net/download/u012881779/10628221
#import "SettingViewController.h"
#import "SDAutoLayout.h"
@interface SettingViewController ()
@end
@implementation SettingViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor lightGrayColor];
[self createView];
}
- (void)createView {
NSArray *imgArray = @[@"ic_sz_ddsz",@"ic_sz_wmsz",@"ic_sz_xtsz",@"ic_sz_ddsz"];
NSArray *titleArray = @[@"店铺详情",@"外卖设置",@"系统设置",@"关于我们"];
UIView *contentView = [[UIView alloc] init];
contentView.backgroundColor = [UIColor clearColor];
[self.view addSubview:contentView];
contentView.sd_layout.leftEqualToView(self.view).topSpaceToView(self.view, 64).rightEqualToView(self.view);
NSMutableArray *viewArray = [[NSMutableArray alloc] init];
for (int i = 0; i < titleArray.count; i ++) {
UIView *inView = [[UIView alloc] init];
inView.backgroundColor = [UIColor whiteColor];
[contentView addSubview:inView];
// 自适应高度,传入高宽比值,label可以传0实现文字高度自适应
// inView.sd_layout.autoHeightRatio(50/375.f);
// 高度值,参数为“(CGFloat)”
inView.sd_layout.heightIs(50);
UIImageView *imgView = [[UIImageView alloc] init];
imgView.image = [UIImage imageNamed:imgArray[i]];
[inView addSubview:imgView];
imgView.sd_layout.leftSpaceToView(inView, 10).centerYEqualToView(inView).heightIs(imgView.image.size.height).widthIs(imgView.image.size.width);
UILabel *titleLabel = [[UILabel alloc] init];
[titleLabel setTextColor:[UIColor blackColor]];
[titleLabel setText:titleArray[i]];
[inView addSubview:titleLabel];
titleLabel.sd_layout.leftSpaceToView(imgView, 10).centerYEqualToView(inView).heightIs(20).rightSpaceToView(inView, 10);
UIButton *button = [[UIButton alloc] init];
[button addTarget:self action:@selector(functionClick:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"" forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"btn_right_arrow"] forState:UIControlStateNormal];
[inView addSubview:button];
// heightRatioToView 高度是参照view高度的多少倍,参数为“(View, CGFloat)”
button.sd_layout.leftEqualToView(inView).topSpaceToView(inView, 0).rightEqualToView(inView).heightRatioToView(inView, 1);
button.titleLabel.sd_layout.leftSpaceToView(button, 10).centerYEqualToView(button).heightIs(20).rightSpaceToView(button, 100);
button.imageView.sd_layout.rightSpaceToView(button, 10).centerYEqualToView(button).heightIs(20).widthIs(20);
UIView *lineView = [[UIView alloc] init];
lineView.backgroundColor = [UIColor blueColor];
[inView addSubview:lineView]; lineView.sd_layout.leftEqualToView(inView).bottomSpaceToView(inView, 0).rightEqualToView(inView).heightIs(0.5);
[viewArray addObject:inView];
}
/**
* UIView 九宫格浮动布局效果
* 设置类似collectionView效果的固定间距自动宽度浮动子view
* viewsArray : 需要浮动布局的所有视图
* perRowItemsCount : 每行显示的视图个数
* verticalMargin : 视图之间的垂直间距
* horizontalMargin : 视图之间的水平间距
* vInset : 上下缩进值
* hInset : 左右缩进值
*/
// 四行一列
// [contentView setupAutoWidthFlowItems:viewArray withPerRowItemsCount:1 verticalMargin:10 horizontalMargin:0 verticalEdgeInset:0 horizontalEdgeInset:0];
// 两行两列
[contentView setupAutoWidthFlowItems:viewArray withPerRowItemsCount:2 verticalMargin:10 horizontalMargin:10 verticalEdgeInset:0 horizontalEdgeInset:0];
}
#pragma mark 设置对应响应事件
- (void)functionClick:(UIButton *)sender {
if (sender.tag == 0) {
// 店铺详情
} else if (sender.tag == 1) {
// 外卖设置
} else {
// 系统设置
}
}
@end
示意图: