網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
首先我們了解下UIStackView ,?UIStackView 其實(shí)是提供了一個(gè)接口用于平鋪一行或一列的視圖組合,繼承自UIView。就相當(dāng)于一個(gè)盒子,里面可以裝任何東西。我們只需要通過(guò)對(duì)stackView的axis
,?distribution
,?alignment
,?spacing
屬性進(jìn)行修改,就可以得到這個(gè)盒子的彈性伸縮。
-??axis(軸向) 屬性決定了 stack 的朝向,只有垂直或水平
-??distribution(分布) 屬性決定了其管理的視圖在沿著其軸向上的布局
-??alignment(對(duì)齊) 屬性決定了其管理的視圖在垂直于其軸向上的布局;
-??spacing(空隙) 屬性決定了其管理的視圖間的最小間隙;
其中?alignment(對(duì)齊)
:
Fill : 垂直方向上鋪滿
Top : 沿頂端對(duì)齊
Center : 沿中心線對(duì)其
Bottom : 沿底部對(duì)齊
First Baseline : 按照第一個(gè)子視圖的文字的第一行對(duì)齊,同時(shí)保證高度最大的子視圖底部對(duì)齊(只在axis為水平方向有效)
Last Baseline : 按照最后一個(gè)子視圖的文字的最后一行對(duì)齊,同時(shí)保證高度最大的子視圖頂部對(duì)齊(只在axis為水平方向有效)
其中distribution(分布)
:
? ? Fill : 鋪滿
? ? Fill Equal : 等寬鋪滿
? ? Fill Proportionally : 等比例鋪滿,需要給各子視圖設(shè)定一個(gè)寬度比或高度比
? ? Equal Spacing :等距離放置
? ? Equal Centering :各個(gè)試圖的中心距離保持一致,不夠放置則壓縮后面的試圖距離;
例如要實(shí)現(xiàn)下圖的效果
for (NSInteger i = 0; i < model.details.count; i++) {
HDGoodsModel * goods = model.details[i];
NSString * key = @"商品SKU";
NSString * count = [NSString stringWithFormat:@"x%ld",(long)goods.count];
SPLabel * leftLabel = [SPLabel label].sFont([UIFont systemFontOfSize:14]).sText(key).sTextColor([UIColor dk_colorWithHexString:@"#777C90"]).sNumberOfLines(1);
if (i != 0) {
leftLabel.textColor = [UIColor clearColor];
}
SPLabel * middleLabel = [SPLabel label].sFont([UIFont systemFontOfSize:14]).sTextAlignment(NSTextAlignmentCenter).sTextColor([UIColor dk_colorWithHexString:@"#20284A"]).sText(goods.specifications).sNumberOfLines(0);
SPLabel * rightLabel = [SPLabel label].sFont([UIFont systemFontOfSize:14]).sTextAlignment(NSTextAlignmentRight).sTextColor([UIColor dk_colorWithHexString:@"#20284A"]).sText(count).sNumberOfLines(1);
UIStackView * stackView = [[UIStackView alloc] initWithArrangedSubviews:@[leftLabel,middleLabel,rightLabel]];
//豎直方向以第一行文字為基準(zhǔn)線
stackView.axis = UILayoutConstraintAxisHorizontal;
stackView.alignment = UIStackViewAlignmentFirstBaseline;
//橫向按等寬度排列
stackView.distribution = UIStackViewDistributionFillProportionally;
stackView.spacing = 0.0;
stackView.backgroundColor = [UIColor clearColor];
//設(shè)置中間的文字是左側(cè)文字的寬度的3.3倍
[middleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(leftLabel.mas_width).multipliedBy(3.3);
}];
//設(shè)置右側(cè)的文字是中間文字寬度的0.2
[rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(middleLabel.mas_width).multipliedBy(0.2);
}];
[self.stackView addArrangedSubview:stackView];
}
?
原文鏈接:https://blog.csdn.net/humiaor/article/details/125898240
相關(guān)推薦
- 2022-07-22 C語(yǔ)言輸出所有水仙花數(shù)
- 2022-06-16 golang組件swagger生成接口文檔實(shí)踐示例_Golang
- 2022-05-31 jQuery實(shí)現(xiàn)側(cè)邊導(dǎo)航欄及滑動(dòng)電梯效果(仿淘寶)_jquery
- 2024-01-31 成功解決 npm ERR! /usr/bin/git ls-remote -h -t git://g
- 2022-04-03 詳解iOS?實(shí)現(xiàn)一對(duì)多代理方案_IOS
- 2022-07-30 @Autowired實(shí)現(xiàn)的原理
- 2022-03-18 Linux系統(tǒng)配置(服務(wù)控制)詳細(xì)介紹_Linux
- 2022-06-12 go實(shí)現(xiàn)grpc四種數(shù)據(jù)流模式_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支