SDAutolayout的使用方法

以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具體情況實現高度自適應。\nGot

it! OH YAEH!這個label會根據這些文字內容高度自適應;而這個父view會根據label和view具體情況實現高度自適應。\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);

[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];

}

cell高度自適應

第一步:在自定義cell的.m文件里面,寫上這個

[selfsetupAutoHeightWithBottomView:_view4bottomMargin:10];

// view4是在cell所有的子視圖里面最下面的view

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

intindex = indexPath.row%5;

NSString*str =_textArray[index];

// >>>>>>>>>>>>>>>>>>>>> * cell自適應步驟2 * >>>>>>>>>>>>>>>>>>>>>>>>

/* model為模型實例,keyPath為model的屬性名,通過kvc統一賦值接口*/ ?keypath:比如你要顯示的是str,str對應的model的屬性是text

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

attributedString

//? --------- attributedString測試:行間距為8 ---------------------------

NSString*text =@"attributedString";

NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStylealloc]init];

[paragraphStylesetLineSpacing: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);

//標注lable的text為attributedString

label.isAttributedContent=YES;

view的動態圓角處理以及UIScrollview的高度自適應,要想顯示的是圓,前提是view的寬高相等

UIScrollView*scrollView = [UIScrollViewnew];

[self.viewaddSubview:scrollView];

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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 參考文檔 比較Masonry 和 SDAutoLayout 兩個自動布局其實各有優劣,在使用上SDAutoLayo...
    wpf_register閱讀 2,056評論 2 2
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,846評論 25 708
  • 蔣勛美學系列,腰封上面有“金句”,是我們認識藝術家的標識語。這本書上是“創世紀以來,只有一個米開朗基羅”。 如果我...
    Enjoy有意閱讀 3,363評論 0 4
  • *導讀: * 如果你只會UITabelView的性能優化,請不要說你懂iOS性能優化,下面是我開發這么多年項目中會...
    柳駿閱讀 2,796評論 4 80
  • 如果雞湯能激勵我前進,那我再喝一碗好了。 昨天和今天,都是晚飯后下到樓下去快步走鍛煉,不僅僅為了控制體重,更為了養...
    邯鄲通閱讀 257評論 0 0