【问题标题】:UICollectionView horizantal scrolling Photo Gallery with swiftUICollectionView 水平滚动照片库与 swift
【发布时间】:2015-01-09 00:36:40
【问题描述】:

我正在尝试使用 UICollectionView 和 Swift 制作照片库。我想全屏显示照片并向右滑动以获得下一张图像。我有一个纵向框架的工作代码,但是一旦我进入横向,我的单元格似乎没有重新调整到正确的位置和大小。

意味着它始终保持纵向屏幕的widthheightx 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()
}
}

【问题讨论】:

    标签: ios swift uicollectionview screen-orientation image-gallery


    【解决方案1】:

    您的问题是您在横向模式下检查方向并使用高度作为宽度。这是不正确的。当设备旋转时,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)
    }
    

    另外,Apple 建议您使用 viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) 方法,因为这是较新的 API。此外,在 iOS9 中,如果您改用此方法,您将使用此方法启用多任务功能。

    【讨论】:

      【解决方案2】:

      由于下一行,可能会打印此错误:

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

      对于快速检查,您可以输入例如:

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

      如果错误消息消失,您应该按照消息中的说明从可用高度(collectionView.bounds.height)中减去 collectionView 的 contentInsets 和 sectionInsets。

      这样做,您将避免单元格高度试图“高于”它的 collectionView 的情况。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-15
        • 2016-09-17
        • 2013-10-18
        • 2016-08-07
        • 2017-02-18
        • 2015-06-08
        相关资源
        最近更新 更多