2016-11-01 6 views

답변

2

당신은 후행, 상부 및 하부 제약을 선도 설정해야합니다. 따라서 width은 화면 너비에 따라 크기가 조정되고 셀에는 동적 높이가 있으므로 이미지의 가로 세로 비율에 따라 height이 설정됩니다. 난 그냥 폭 제약에 초점을 맞추고

1

은 (& 후행를 선도),하지만 당신은 최고 & 하단에 비슷한 방식으로 다른 제약 조건을 조정할 수 있습니다.

또한 이미지보기를 변경하여이를 지원할 수도 있지만 사용하는 이미지에 따라 다릅니다. Aspect Fill을 선택하여 전체 폭을 사용했는지 확인합니다. 이미지의 크기가 적절하지 않은 경우이 설정은 상단에있는 & 하단을 잘라냅니다. 그들은으로 선택 표시 왼쪽 & 우측 상자가 빨간색으로 라인을 추가 새로운 제약 조건 창에서

enter image description here

알 수 있습니다.

필요에 따라 이미지를 조정하여 링크 된 이미지와 같이 패딩을 제공 할 수 있습니다. 제약 조건을 설정하는 방법은 여전히 ​​동일하지만 상자의 값만 다릅니다.

1

먼저 'UITableViewAutomaticDimension'을 사용하여 셀의 높이를 설정할 때 UIImageView가 작동하지 않는다는 것을 알아야합니다.

'높이 제한'을 설정하고 값을 변경하면 값이 작동합니다.

그래서, '최고', '바닥'과 이미지보기

와 그리고 당신의 이미지를 다운로드 한 경우,이 크기의 수와 수학을하고 '높이'제약 '후행'의 '선도'설정 그것으로 '높이 제한'을 설정하십시오.

enter image description here

내 나쁜 영어 죄송
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
{ 
    return UITableViewAutomaticDimension 
} 

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat 
{ 
    return UITableViewAutomaticDimension 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 
    let cell = tableView.dequeueReusableCell(withIdentifier: "list") 

    if let iv = cell?.contentView.viewWithTag(1) as? UIImageView 
    { 
     // this UIImageView.image(url: URL, placeHolder: UIImage, complete: (UIImage)->()) is my custom function. 
     // you can use SDWebImage's function 
     iv.image(url: URL(string: "http://imageurl")!, placeHolder: nil, complete: { (img) in 

      // calculate imageview's height 
      let ivHeight = (iv.frame.size.width * img.size.height)/img.size.width 

      // get height constraint and set the value. 
      for lc in iv.constraints 
      { 
       if lc.firstAttribute == .height 
       { 
        // this will change cell's height automatically 
        lc.constant = ivHeight 
        break 
       } 
      } 
     }) 
    } 

    return cell! 
} 

. :(

관련 문제