2016-09-16 152 views
0

我在每个单元格中都有tableview单元格collectionView。在collectionView中只有图像。可能有一个或多个 如果显示5张图像的布局,则可以。当我显示1图像时需要根据图像的宽度和高度调整collectionView的大小。所以我可以显示肖像图像和风景图像。我目前的布局是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) 
     } 
    } 

我使用默认CollectionViewFlowLayout 能否请你帮我这个问题?

+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

相关问题