当前位置: 首页 > 工具软件 > SDAutoLayout > 使用案例 >

SDAutolayout的使用方法

闻人昊昊
2023-12-01

1、导入

SDAutoLayout的GitHub地址:https://github.com/gsdios/SDAutoLayout

支持pod: pod 'SDAutoLayout', '~> 2.1.3'

完整微信Demo https://github.com/gsdios/GSD_WeiXin

2、使用

(1)综合示例

 *******************************************************************************
        
        注意:先把需要自动布局的view加入父view然后在进行自动布局,例: 

        UIView *view0 = [UIView new];
        UIView *view1 = [UIView new];
        [self.view addSubview:view0];
        [self.view addSubview:view1];
        
        view0.sd_layout
        .leftSpaceToView(self.view, 10)
        .topSpaceToView(self.view, 80)
        .heightIs(100)
        .widthRatioToView(self.view, 0.4);
        
        view1.sd_layout
        .leftSpaceToView(view0, 10)
        .topEqualToView(view0)
        .heightRatioToView(view0, 1)
        .rightSpaceToView(self.view, 10);

    *******************************************************************************

 

(2)UILabel文字自适应

// autoHeightRatio() 传0则根据文字自动计算高度(传大于0的值则根据此数值设置高度和宽度的比值)
    _label.sd_layout.autoHeightRatio(0);

(3)tableview和cell高度自适应:

普通(简化)版【推荐使用】:tableview 高度自适应设置只需要2步

  1. >> 设置cell高度自适应:
    //在自定义cell的.m文件里面写
    // cell布局设置好之后调用此方法就可以实现高度自适应(注意:如果用高度自适应则不要再以cell的底边为参照去布局其子view)
// view4是在cell所有的子视图里面最下面的view

    [cell setupAutoHeightWithBottomView:_view4 bottomMargin:10];
    2. >> 获取自动计算出的cell高度 

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    // 获取cell高度
    return [self cellHeightForIndexPath:indexPath cellContentViewWidth:[UIScreen mainScreen].bounds.size.width];
    }

3、示例

以UIView为例:

UIView *view1 = [UIView new];

view1.sd_layout都是点语法

距离左边:

view1.sd_layout.leftSpaceToView(self.view,20)

参数1是view1左边以哪个视图为基准,参数2是具体的数值距离上下右就把left换成对应的位置就行

对齐:

leftEqualToView(self.view,20)

需要其它对齐,将左变成需要的位置

宽高:

(1)具体数值

heightIs(100)widthIs(20)

(2)根据其他视图的比例来:

heightRatioToView(self.view,0.5)

view1的高是view的0.5倍,就是一半

widthRatioToView(self.view,0.5)

view1的宽是view的0.5倍,就是一半

中心:

centerYEqualToView(self.view)centerXEqualToView(self.view)

Label内容自适应:

① label的父视图根据label的高度自适应

UILabel*subview1 = [UILabelnew];//初始化子label

subview1.text=@"这个label会根据这些文字内容高度自适应;而这个父view会根据label和view具体情况实现高度自适应";
UIView*subview2 = [UIViewnew];//初始化子view2

subview2.backgroundColor= [UIColororangeColor];

//将子view添加进父view

[self.view1sd_addSubviews:@[subview1,

subview2]];

subview1.sd_layout

.leftSpaceToView(self.view1,10)

.rightSpaceToView(self.view1,10)

.topSpaceToView(self.view1,10)

.autoHeightRatio(0);//设置文本内容自适应,如果这里的参数为大于0的数值则会以此数值作为view的高宽比设置view的高度

subview2.sd_layout

.topSpaceToView(subview1,10)

.widthRatioToView(subview1,1)

.heightIs(30)

.leftEqualToView(subview1);

//view1使用高度根据子view内容自适应,所以不需要设置高度,而是设置“[self.view1 setupAutoHeightWithBottomView:testView bottomMargin:10];”实现高度根据内容自适应

self.view1.sd_layout

.leftSpaceToView(self.view,10)

.topSpaceToView(self.view,80)

.rightSpaceToView(self.view,10);

//设置view1高度根据子其内容自适应

[self.view1setupAutoHeightWithBottomView:subview2bottomMargin:10];

②label宽度自适应

UILabel*autoWidthlabel = [UILabelnew];

autoWidthlabel.backgroundColor= [[UIColororangeColor]colorWithAlphaComponent:0.5];

_autoWidthLabel= autoWidthlabel;

autoWidthlabel.font= [UIFontsystemFontOfSize:12];

autoWidthlabel.text=@"宽度自适应(距离父view右边距10)";

[self.viewaddSubview:autoWidthlabel];

autoWidthlabel.sd_layout

.rightSpaceToView(self.view,10)

.heightIs(20)

.bottomSpaceToView(self.view,50);

[autoWidthlabelsetSingleLineAutoResizeWithMaxWidth:180];

③label高度自适应

UILabel*autoHeightlabel = [UILabelnew];

autoHeightlabel.backgroundColor= [[UIColorredColor]colorWithAlphaComponent:0.5];

autoHeightlabel.font= [UIFontsystemFontOfSize:12];

autoHeightlabel.text=@"高度自适应(距离父view左边距10,底部和其右侧label相同,宽度为100)";

[self.viewaddSubview:autoHeightlabel];

autoHeightlabel.sd_layout

.bottomEqualToView(_autoWidthLabel)

.leftSpaceToView(self.view,10)

.widthIs(100)

.autoHeightRatio(0);

设置一排固定间距自动宽度子view

- (void)setupAutoWidthViewsWithCount:(NSInteger)count margin:(CGFloat)margin

{

_autoWidthViewsContainer= [UIViewnew]; //放button的父视图

_autoWidthViewsContainer.backgroundColor= [UIColorgreenColor];

[self.viewaddSubview:_autoWidthViewsContainer];

NSMutableArray*temp = [NSMutableArraynew];

for(inti =0; i < count; i++) {

UIView*view = [UIViewnew];

view.backgroundColor= [UIColororangeColor];

[_autoWidthViewsContaineraddSubview:view];

view.sd_layout.autoHeightRatio(0.5);//设置高度约束

[tempaddObject:view];

}

_autoWidthViewsContainer.sd_layout

.leftSpaceToView(self.view,10)

.rightSpaceToView(self.view,10)

.topSpaceToView(_centerButton,10);

//此步设置之后_autoWidthViewsContainer的高度可以根据子view自适应

[_autoWidthViewsContainersetupAutoWidthFlowItems:[tempcopy]withPerRowItemsCount:countverticalMargin:marginhorizontalMargin:margin];

}

设置一排固定宽度自动间距子view

- (void)setupAutoMarginViewsWithCount:(NSInteger)count itemWidth:(CGFloat)itemWidth

{

_autoMarginViewsContainer= [UIViewnew];

_autoMarginViewsContainer.backgroundColor= [UIColorblueColor];

[self.viewaddSubview:_autoMarginViewsContainer];

NSMutableArray*temp = [NSMutableArraynew];

for(inti =0; i < count; i++) {

UIView*view = [UIViewnew];

view.backgroundColor= [UIColororangeColor];

[_autoMarginViewsContaineraddSubview:view];

view.sd_layout.autoHeightRatio(0.5);//设置高度约束

[tempaddObject:view];

}

_autoMarginViewsContainer.sd_layout

.leftSpaceToView(self.view,10)

.rightSpaceToView(self.view,10)

.topSpaceToView(_autoWidthViewsContainer,10);

//此步设置之后_autoMarginViewsContainer的高度可以根据子view自适应

[_autoMarginViewsContainersetupAutoMarginFlowItems:[tempcopy]withPerRowItemsCount:countitemWidth:itemWidthverticalMargin:10];

}

 

 

 类似资料: