2016-09-16 4 views
0

각 셀 안에 tableview 셀 컬렉션 뷰가 있습니다. collectionView에서는 이미지 만 표시합니다. 하나 이상의 이미지가있을 수 있습니다. 5 개의 이미지 레이아웃을 표시하면 괜찮습니다. 1 이미지를 표시 할 때 이미지의 너비와 높이에 따라 collectionView의 크기를 조정해야 할 때의 문제입니다. 그래서 나는 초상화 이미지와 풍경 이미지를 보여줄 수있다. 나의 현재의 레이아웃은 당신이이 문제를 좀 도와 주 시겠어요 기본 CollectionViewFlowLayout 을 사용하고UICollectionView 셀 자동 크기 조정

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 

     switch images.count { 
     case 1: 
      let size = collectionView.frame.size.width 
      return CGSizeMake(size, 250) 
     default: 
      let size = collectionView.frame.size.width/5 
      let offset = CGFloat(5) 
      return CGSizeMake(size - offset, size - offset) 
     } 
    } 

입니까?

+0

위의 코드로 인해 겪고있는 문제점을 설명 할 수 있습니까? – Natarajan

+0

사진이 컬렉션보기 내부에있는 단일 사진에 문제가 있습니다. 사진 콘텐츠 (세로 또는 가로 사진 방향 포함)에 따라 크기 조정 컬렉션보기가 필요합니다. – Anton

답변

0

아래에 몇 가지 해결 방법이 추가되었습니다. 그것을 확인하시기 바랍니다. 아래 코드는 Swift 3.0에 있습니다.

var collectionView:UICollectionView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    let layout = UICollectionViewFlowLayout() 
    layout.minimumLineSpacing = 0.0 
    layout.minimumInteritemSpacing = 0.0 

    collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout) 
    collectionView.translatesAutoresizingMaskIntoConstraints = false 
    collectionView.delegate = self 
    collectionView.dataSource = self 
    self.view.addSubview(collectionView) 

    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[collectionView]|", options: NSLayoutFormatOptions(rawValue:0), metrics: nil, views: ["collectionView":collectionView])) 
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[collectionView]|", options: NSLayoutFormatOptions(rawValue:0), metrics: nil, views: ["collectionView":collectionView])) 

    //Register your cell to collectionView over here 

} 

//Add this method to support for orientation changes. 
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { 

    coordinator.animate(alongsideTransition: {(context) -> Void in 

    }) {[weak self] (context) -> Void in 

     self?.collectionView.performBatchUpdates(nil, completion: nil) 
    } 
} 

//Calling collectionView.performBatchUpdates will call the bellow method where you can able to adjust your cell size for orientation change 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

    switch images.count { 
    case 1: 
     let size = collectionView.frame.size.width 
     return CGSizeMake(size, 250) 
    default: 
     let size = collectionView.frame.size.width/5 
     let offset = CGFloat(5) 
     return CGSizeMake(size - offset, size - offset) 
    } 
} 

또한 CollectionViewCell에있는 ImageView는 CollectionViewCell의 컨텐츠보기의 왼쪽 및 오른쪽 여백에 고정해야합니다. 이미지 종횡비가 collectionView 폭의 폭과 일치하는지 확인하거나 ImageView의 contentMode를 scaleToFill로 설정하십시오.

희망이 있습니다.

감사합니다.

+0

기기를 회전 할 때 질문이 아닙니다. 당신은 초상화와 풍경 모드로 이미지를 가지고 질문. 예를 들어, 사진 가로가 세로보다 작아 지도록 장치를 수직으로 찍은 사진을 찍습니다. 이미지의 크기에 맞게 크기를 조정해야합니다. – Anton