2017-09-10 36 views
0

我想以適合屏幕寬度的編程方式在表格視圖中顯示遠程圖像列表。使用tableView的圖像列表Autolayout錯誤查看

我的表格視圖有這個約束:tableView.rowHeight = UITableViewAutomaticDimension

我創建一個單元格,添加一個圖像並創建約束。

let cell = UITableViewCell() 
let url = URL(string: "https://static.pexels.com/photos/411089/pexels-photo-411089.jpeg") 
let data = NSData(contentsOf: url!) 
let imageView = UIImageView() 
cell.addSubview(imageView) 
imageView.image = UIImage(data: data as Data!) 
imageView.translatesAutoresizingMaskIntoConstraints = false 

imageView.leadingAnchor.constraint(equalTo: cell.leadingAnchor).isActive = true 
imageView.trailingAnchor.constraint(equalTo: cell.trailingAnchor).isActive = true 
imageView.topAnchor.constraint(equalTo: cell.topAnchor).isActive = true 
imageView.bottomAnchor.constraint(equalTo: cell.bottomAnchor).isActive = true 
return cell 

而且我得到這個(部分照片):

Photos section - distorted

圖像渲染不正確,高度過大。我試圖修復它,並添加比例的限制:

let ratio = imageView.image!.size.height/imageView.image!.size.width 
imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: ratio).isActive = true 

而在這之後,圖像顯示正確的,但我得到約束非致命錯誤: good layout, but with constraint warning

誤差爲每個圖像:

2017-09-10 23:50:52.490946+0300 MyHelloWorld[35126:1135489] [LayoutConstraints] 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. 
(
    "<NSLayoutConstraint:0x60800009eaa0 UIImageView:0x7fe4605236e0.height == 0.667391*UIImageView:0x7fe4605236e0.width (active)>", 
    "<NSLayoutConstraint:0x60800009ec30 H:|-(0)-[UIImageView:0x7fe4605236e0] (active, names: '|':UITableViewCell:0x7fe4608a0c00)>", 
    "<NSLayoutConstraint:0x60800009ecd0 UIImageView:0x7fe4605236e0.trailing == UITableViewCell:0x7fe4608a0c00.trailing (active)>", 
    "<NSLayoutConstraint:0x60800009ed20 V:|-(0)-[UIImageView:0x7fe4605236e0] (active, names: '|':UITableViewCell:0x7fe4608a0c00)>", 
    "<NSLayoutConstraint:0x60800009edc0 UIImageView:0x7fe4605236e0.bottom == UITableViewCell:0x7fe4608a0c00.bottom (active)>", 
    "<NSLayoutConstraint:0x60800009efa0 'UIView-Encapsulated-Layout-Height' UITableViewCell:0x7fe4608a0c00.height == 213.5 (active)>", 
    "<NSLayoutConstraint:0x60800009eeb0 'UIView-Encapsulated-Layout-Width' UITableViewCell:0x7fe4608a0c00.width == 320 (active)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60800009eaa0 UIImageView:0x7fe4605236e0.height == 0.667391*UIImageView:0x7fe4605236e0.width (active)> 

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. 

我不知道這個約束 - 異常意味着什麼。我不知道如何解決它。請幫忙,我已經花了幾天時間來處理這個異常。

我該如何解決這個錯誤?

謝謝。

回答

1

您應該將約束的優先級從1000設置爲999.這樣,可以首先應用表格視圖單元的封裝佈局尺寸。

let imageRatioConstraint = imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: ratio) 
imageRatioConstraint.priority = 999 
imageRatioConstraint.isActive = true 
+0

是的,修好了!非常感謝! – comm1x