4

我正在試圖使用UICollectionView與斯威夫特照片庫。我想全屏顯示照片,然後向右滑動即可看到下一張圖像。我有一個肖像框架的工作代碼,但只要我去風景看起來像我的細胞不會重新調整自己的正確位置和大小。UICollectionView horizantal滾動照片庫與swift

這意味着它始終保持肖像畫面的相同width,height,x positiony position。我也得到縱向和橫向框架此錯誤:

- >沒有定義UICollectionViewFlowLayout的行爲,因爲:

- >項目高度必須小於該UICollectionView減去部分插圖的高度頂部和底部值。

我已經在網上搜索了一個答案,並嘗試了所有可能的解決方案,但它似乎並沒有爲我工作。讓我知道是否有人可以幫助這個。非常感激。

這裏是我的代碼:

import UIKit 

class ImageDetailViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,UINavigationControllerDelegate { 


@IBOutlet weak var imgDetailCollectionView: UICollectionView! 

var index: Int! 
var imageItems: Array<Items> = [] 
var currentIndex: CGFloat! 
var size = UIScreen.mainScreen().bounds.size 
init(index: Int , imageItems: Array<Items>) { 
    self.index = index 
    self.imageItems = imageItems 
    super.init(nibName: "ImageDetailViewController", bundle: NSBundle.mainBundle()) 
} 

required init(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 


    imgDetailCollectionView.registerNib(UINib(nibName: "ImageDetailCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ImageDetailCollectionViewCell") 
    //self.automaticallyAdjustsScrollViewInsets = false 
    imgDetailCollectionView.setZoomScale(0.5, animated: true) 

} 




override func viewDidLayoutSubviews() { 
    imgDetailCollectionView.scrollRectToVisible(CGRectMake(320 * CGFloat(index), 0, 320 , 240), animated: false) 
} 



override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{ 

    return self.imageItems.count 
} 


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{ 






    var cell = imgDetailCollectionView.dequeueReusableCellWithReuseIdentifier("ImageDetailCollectionViewCell", forIndexPath: indexPath) as ImageDetailCollectionViewCell 

    cell.setup(imageItems[indexPath.row].url!) 

    if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation)){ 


     self.imgDetailCollectionView.collectionViewLayout.invalidateLayout() 

     var frame = cell.frame 
     frame.size = CGSizeMake(size.width, size.height) 
     cell.frame = frame 

    }else{ 
     self.imgDetailCollectionView.collectionViewLayout.invalidateLayout() 

     var frame = cell.frame 
     frame.size = CGSizeMake(size.height, size.width) 
     cell.frame = frame 
    } 

    return cell 

} 


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

    var size = UIScreen.mainScreen().bounds.size 

    if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation)){ 


     return CGSizeMake(size.width, size.height) 


    }else{ 

     return CGSizeMake(size.height, size.width) 
    } 



} 

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat { 
    return 0 
} 

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat { 
    return 0 
} 


override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) { 
    self.imgDetailCollectionView.collectionViewLayout.invalidateLayout() 

    var currentOffset = self.imgDetailCollectionView.contentOffset 
    self.currentIndex = currentOffset.x/imgDetailCollectionView.frame.size.width 

} 


override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) { 

    var currentSize = imgDetailCollectionView.bounds.size 
    var offset = self.currentIndex * currentSize.width 
    imgDetailCollectionView.setContentOffset(CGPointMake(offset, 0), animated: false) 

    imgDetailCollectionView.reloadData() 
} 

override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) { 

    self.imgDetailCollectionView.collectionViewLayout.invalidateLayout() 
} 
} 

回答

0

你的問題是要檢查的方向,並使用高度時,它是在橫向模式的寬度。這是不正確的。當設備旋轉時,width屬性將返回設備縱向的實際高度。

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    var cell = imgDetailCollectionView.dequeueReusableCellWithReuseIdentifier("ImageDetailCollectionViewCell", forIndexPath: indexPath) as ImageDetailCollectionViewCell 
    cell.setup(imageItems[indexPath.row].url!) 
    cell.frame.size = view.frame.size 
    return cell 
} 
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    return CGSizeMake(view.frame.width, view.frame.height) 
} 

而且另注,蘋果建議您使用viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)方法,因爲這是較新的API。另外,如果使用此方法,則在iOS9中,您將使用此方法啓用多任務功能。

0

可能因爲下一行的打印此錯誤:

變種大小= UIScreen.mainScreen()bounds.size

對於快速檢查可以鍵入例如:

變種大小= UIScreen.mainScreen()。bounds.size * 0.25

如果錯誤消息消失,則應遵循消息中的指示信息,並從可用高度(collectionView.bounds.height)中減去collectionView的contentInsets和sectionInsets。

這樣做,您將避免單元格高度比'collectionView'更高的情況。