日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS AutoLayout使用技巧

發(fā)布時間:2023/12/19 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS AutoLayout使用技巧 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

關(guān)于ContentCompressionResistance, ContentHugging運用

如下圖效果圖,兩個Label并列在同一排上,左邊label自適應(yīng),右邊label(紅色)要使得內(nèi)容全部展示,如果左邊label內(nèi)容很少,那么右邊label隨著左邊label動。

使用Snapkit 約束實現(xiàn)效果

使用xib AutoLayout 約束實現(xiàn)效果

xib AutoLayout關(guān)鍵設(shè)置

1.設(shè)置好相關(guān)約束,詳見demo.

2.把左邊label Compression horizontal降低至250(默認(rèn)750,設(shè)置低于750任意值均可),右邊紅色label無需修改。

如下圖

Snapkit 約束關(guān)鍵代碼

import UIKit import SnapKitclass SnpTableViewCell: UITableViewCell {var placeLabel = UILabel()var distanceLabel = UILabel()override init(style: UITableViewCellStyle, reuseIdentifier: String?) {super.init(style: style, reuseIdentifier: reuseIdentifier)setupUI()}required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented")}override func awakeFromNib() {super.awakeFromNib()}/// MARK: -setup UIfunc setupUI() {distanceLabel.textColor = UIColor.redif #available(iOS 8.2, *) {distanceLabel.font = UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.semibold)} else {distanceLabel.font = UIFont.boldSystemFont(ofSize: 17)}contentView.addSubview(placeLabel)contentView.addSubview(distanceLabel)placeLabel.snp.makeConstraints {$0.left.equalToSuperview().offset(16)$0.top.bottom.equalToSuperview()}distanceLabel.snp.makeConstraints{$0.top.bottom.equalToSuperview()$0.left.equalTo(placeLabel.snp.right).offset(32)$0.right.lessThanOrEqualToSuperview().offset(-16)}//set CompressionResistance ContentHuggingdistanceLabel.setContentCompressionResistancePriority(.required, for: .horizontal)distanceLabel.setContentHuggingPriority(.required, for: .horizontal)placeLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)placeLabel.setContentHuggingPriority(.required, for: .horizontal)}override func setSelected(_ selected: Bool, animated: Bool) {super.setSelected(selected, animated: animated)// Configure the view for the selected state}} 復(fù)制代碼

關(guān)鍵設(shè)置代碼是:

//set CompressionResistance ContentHuggingdistanceLabel.setContentCompressionResistancePriority(.required, for: .horizontal)distanceLabel.setContentHuggingPriority(.required, for: .horizontal)placeLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)placeLabel.setContentHuggingPriority(.required, for: .horizontal) 復(fù)制代碼

關(guān)于詳細(xì)實現(xiàn)過程,及原理待續(xù)。。。。

代碼地址點擊這里

總結(jié)

以上是生活随笔為你收集整理的iOS AutoLayout使用技巧的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。