因?yàn)椴煌臋C(jī)型的長(zhǎng)寬比不一樣士飒,導(dǎo)致同樣的圖片難于適應(yīng)所有的屏幕刊橘。一般情況下暇矫,需求是image view的寬度跟屏幕一樣主之,根據(jù)實(shí)際情況調(diào)整image view的高度。比如一張685*309
像素的圖片李根,在iPhone6上的size是375*167
槽奕,而在iPhone6 Plus上則是414*186
。為了達(dá)到最優(yōu)的顯示效果房轿,必須根據(jù)圖片的size動(dòng)態(tài)調(diào)整cell的高度粤攒。
圖片的size可以從URL獲取,也可以在SDWebImage這樣的庫(kù)里面的completion block里面獲取囱持,當(dāng)然為了達(dá)到最好的效果夯接,在URL中指明圖片的size是更好的。
在渲染cell的時(shí)候纷妆,先根據(jù)圖片URL的寬長(zhǎng)比钻蹬,得到imageview合理的高度,然后給imageview設(shè)置上這個(gè)高度約束凭需。我發(fā)現(xiàn)一個(gè)有趣的問(wèn)題问欠,就是AutoLayout會(huì)影響UIImageView的contentMode,即使指定了ScaleAspectFit
粒蜈,同樣會(huì)拉伸圖片顺献,造成變形。
final class TestViewCellUseConstraint : UITableViewCell {
var imgViewHeightConstraint : Constraint?
lazy var imgView : UIImageView = {
let imgView = UIImageView()
imgView.contentMode = UIViewContentMode.ScaleAspectFill
imgView.clipsToBounds = true
self.contentView.addSubview(imgView)
return imgView
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.contentView.backgroundColor = UIColor.redColor()
self.imgView.snp_makeConstraints { (make) -> Void in
make.leading.equalTo(0)
make.trailing.equalTo(0)
make.top.equalTo(8)
//給一個(gè)默認(rèn)的高度值
self.imgViewHeightConstraint = make.height.equalTo(154.5).constraint
make.bottom.equalTo(self.contentView.snp_bottom).offset(0)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
final class TestViewController : YWSBaseUITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.separatorStyle = .None
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let image = UIImage(named: "aliyun_sz")!
let cell = TestViewCellUseConstraint()
cell.imgView.image = image
cell.imgViewHeightConstraint?.updateOffset(floor(DeviceUtils.getScreenWidth()/(image.size.width / image.size.height)))
cell.setNeedsUpdateConstraints()
return cell
}
}
最后需要注意的是枯怖,在計(jì)算高度的時(shí)候應(yīng)該避免出現(xiàn)少數(shù)點(diǎn)的約束注整,否則可能產(chǎn)生下面這樣的約束沖突。image view的高度計(jì)算出來(lái)是186.753280639648
,而content view計(jì)算出來(lái)的高度則是186.667
肿轨,二者不一樣寿冕,但是我指明二者的高度是一樣的,iOS會(huì)選擇丟掉一個(gè)約束椒袍。
2016-03-16 18:02:06.930 CloudConsoleApp[74807:15177377] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<SnapKit.LayoutConstraint:0x7fab951e80d0@/Users/henshao/cloudconsole-iOS/CloudConsoleApp/YWSMyViewController.swift#336 UIImageView:0x7fab95537d80.top == UITableViewCellContentView:0x7fab95537970.top>",
"<SnapKit.LayoutConstraint:0x7fab951e8620@/Users/henshao/cloudconsole-iOS/CloudConsoleApp/YWSMyViewController.swift#336 UIImageView:0x7fab95537d80.height == 186.753280639648>",
"<SnapKit.LayoutConstraint:0x7fab951e8a60@/Users/henshao/cloudconsole-iOS/CloudConsoleApp/YWSMyViewController.swift#336 UIImageView:0x7fab95537d80.bottom == UITableViewCellContentView:0x7fab95537970.bottom>",
"<NSLayoutConstraint:0x7fab951e9940 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fab95537970(186.667)]>"
)
Will attempt to recover by breaking constraint
<SnapKit.LayoutConstraint:0x7fab951e8620@/Users/henshao/cloudconsole-iOS/CloudConsoleApp/YWSMyViewController.swift#336 UIImageView:0x7fab95537d80.height == 186.753280639648>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
UILabel的長(zhǎng)度和寬度是根據(jù)字體自適應(yīng)的驼唱,如果UIImageView也擁有這種特性就好了啊,指定寬度驹暑,高度上的約束自適應(yīng)玫恳,省多少事啊。