SDAutolayout用法大全

来源:互联网 发布:淘宝已经收货申请退款 编辑:程序博客网 时间:2024/06/01 08:15

以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会根据labelview具体情况实现高度自适应。\nGot it! OH YAEH! 这个label会根据这些文字内容高度自适应;而这个父view会根据labelview具体情况实现高度自适应。\nGot it! OH YAEH!";

   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);

    

    [autoWidthlabel setSingleLineAutoResizeWithMaxWidth: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 (int i =0; i < count; i++) {

        UIView *view = [UIViewnew];

        view.backgroundColor = [UIColororangeColor];

        [_autoWidthViewsContaineraddSubview:view];

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

        [temp addObject: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 (int i =0; i < count; i++) {

        UIView *view = [UIViewnew];

        view.backgroundColor = [UIColororangeColor];

        [_autoMarginViewsContaineraddSubview:view];

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

        [temp addObject:view];

    }

    _autoMarginViewsContainer.sd_layout

    .leftSpaceToView(self.view10)

    .rightSpaceToView(self.view10)

    .topSpaceToView(_autoWidthViewsContainer10);


    

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

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

}


cell高度自适应

第一步:在自定义cell的.m文件里面,写上这个 

[selfsetupAutoHeightWithBottomView:_view4bottomMargin:10]; // view4是在cell所有的子视图里面最下面的view

第二步:在返回cell高度的方法里面

    int index = indexPath.row %5;

    NSString *str = _textArray[index];

    

    // >>>>>>>>>>>>>>>>>>>>> * cell自适应步骤2 * >>>>>>>>>>>>>>>>>>>>>>>>

    /* model 为模型实例, keyPath model的属性名,通过 kvc统一赋值接口 */  keypath:比如你要显示的是str,str对应的model的属性是text

    return [self.tableViewcellHeightForIndexPath:indexPathmodel:str keyPath:@"text"cellClass:[TestCell2class]contentViewWidth:[selfcellContentViewWith]];


attributedString

//  --------- attributedString测试:行间距为8 ---------------------------

    

   NSString *text =@"attributedString测试:行间距为8。彩虹网络卡福利费绿调查开房;卡法看得出来分开了的出口来反馈率打开了房;快烦死了;了;调查开房;;v单纯考虑分离开都快来反馈来看发v离开的积分房积分jdhflgfkkvvm.cmattributedString测试:行间距为8。彩虹网络卡福利费绿调查开房;卡法看得出来分开了的出口来反馈率打开了房;快烦死了;了;调查开房;;v单纯考虑分离开都快来反馈来看发v离开的积分房积分jdhflgfkkvvm.cm";

    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStylealloc]init];

    [paragraphStyle setLineSpacing:8];

    UIColor *color = [UIColorblackColor];

    NSAttributedString *string = [[NSAttributedStringalloc]initWithString:textattributes:@{NSForegroundColorAttributeName : color,NSParagraphStyleAttributeName: paragraphStyle}];

    

    

    UILabel *label = [UILabelnew];

    [self.viewaddSubview:label];

    label.attributedText = string;

    

    label.sd_layout

    .leftSpaceToView(self.view,10)

    .rightSpaceToView(self.view,10)

    .topSpaceToView(self.view,70)

    .autoHeightRatio(0);

    

    // 标注labletextattributedString

    label.isAttributedContent =YES;

view的动态圆角处理以及UIScrollview的高度自适应,要想显示的是圆,前提是view的宽高相等

UIScrollView *scrollView = [UIScrollViewnew];

    [self.viewaddSubview:scrollView];

    

    [scrollView sd_addSubviews:@[self.view0,self.view1,self.view2,self.view3,self.view4,self.view5,self.view6,self.view7,self.view8]];

    

    

    scrollView.sd_layout.spaceToSuperView(UIEdgeInsetsZero);

    

    self.view0.sd_layout

    .leftSpaceToView(scrollView,20)

    .rightSpaceToView(scrollView,20)

    .topSpaceToView(scrollView,20)

    .heightIs(150);

    

    self.view1.sd_layout

    .widthIs(200)

    .heightIs(200)

    .centerXEqualToView(scrollView)

    .topSpaceToView(self.view0,20);

    

    self.view2.sd_layout

    .leftSpaceToView(scrollView,50)

    .rightSpaceToView(scrollView,50)

    .topSpaceToView(self.view1,20)

    .heightIs(150);

    

    self.view3.sd_layout

    .widthIs(250)

    .heightEqualToWidth()

    .centerXEqualToView(scrollView)

    .topSpaceToView(self.view2,20);


    // scrollview自动contentsize

    [scrollViewsetupAutoContentSizeWithBottomView:self.view3bottomMargin:20];

    

    // 设置圆角

    self.view0.sd_cornerRadiusFromHeightRatio = @(0.5); // 设置view0的圆角半径为自身高度的0.5

    self.view1.sd_cornerRadiusFromWidthRatio =@(0.5);

    self.view2.sd_cornerRadiusFromWidthRatio =@(0.5);


好,就研究这么多,再深入的还没有用到


PS:如果需要一些动画的话,就是简单的变坐标或者是变大小,可以再调完之后加上updateLayout

1 0
原创粉丝点击