[iOS]SDAutoLayout简单使用

介绍了工具SDAutoLayout,它支持Cell和Tableview高度自适应,以及Label和ScrollView内容自适应。还给出了该工具的GitHub地址和本Demo的下载地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

工具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

示意图:



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值